• 物联网通信RESTDemo示例程序(Python版本)


    源码下载 

     

    app.py

    #!/usr/bin/python

    # -*- coding:utf-8 -*-

     

    from commondlib.help.ini import *

    from commondlib.help.mysql import *

    from commondlib.help.data import *

    from commondlib.help.txt import *

    from flask import Flask, abort, request, jsonify

    import json

     

    __name__ = '__main__'

     

    app = Flask(__name__)

     

    def init():

        # log

        CTxtHelper.init("restserver")

     

        CTxtHelper.appendlog("soft start")

     

        # ini

        status = CIniHelper.getfile("config.ini")

        if not status:

            return False

     

        node = "systemparam"

        dbip = CIniHelper.read(node, "dbip")

        dbname = CIniHelper.read(node, "dbname")

        dbuser = CIniHelper.read(node, "dbuser")

        dbpass = CIniHelper.read(node, "dbpass")

     

        # mysql

        status = CMySqlHelper.init(dbip, dbuser, dbpass, dbname)

        if not status:

            return False

     

        return True

     

     

    @app.route('/test/', methods=['GET'])

    def test():

        return jsonify({'code': 0, 'info': "ok", 'errmsg': "", 'data': "[]"})

     

     

    @app.route('/query/', methods=['POST'])

    def query():

        if not request.json or 'sql' not in request.json:

            abort(400)

        code = 1

        info = "success"

        outmsg = []

        sql = CDataHelper.fromate_utf(request.json['sql'])

        sql = sql.replace("\", "")

        data = CMySqlHelper.query(sql, outmsg)

        errmsg = CDataHelper.list2string(outmsg)

        if not CDataHelper.string_empty(errmsg):

            code = 1000

            info = "fail"

        out = {'code': code, 'info': info, 'errmsg': errmsg, 'data': data}

        CTxtHelper.appendlog("[Info] rest_query,input:" + sql + ",output:" + json.dumps(out))

        return jsonify(out)

     

     

    @app.route('/execute/', methods=['POST'])

    def execute():

        if not request.json or 'sql' not in request.json:

            abort(400)

        code = 1

        info = "success"

        outmsg = []

        sql = CDataHelper.fromate_utf(request.json['sql'])

        sql = sql.replace("\", "")

        status = CMySqlHelper.execute(sql, outmsg)

        errmsg = CDataHelper.list2string(outmsg)

        if not status:

            code = 1000

            info = "fail"

        out = {'code': code, 'info': info, 'errmsg': errmsg, 'data': "[]"}

        CTxtHelper.appendlog("[Info] rest_execute,input:" + sql + ",output:" + json.dumps(out))

        return jsonify(out)

     

     

    if __name__ == '__main__':

        init()

        app.run(True)

        #app.run("0.0.0.0", 8080, True)

     

    httpclient.py

    #!/usr/bin/python

    # -*- coding:utf-8 -*-

     

    import urllib2, urllib

     

    from commondlib.help.txt import CTxtHelper

     

    class CHttpHelper:

     

        @staticmethod

        def __init__():

            return

     

        @staticmethod

        def post(url, data):

            ret = ''

            try:

                req = urllib2.Request(url)

                data = urllib.urlencode(data)

                opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())

                response = opener.open(req, data)

                ret = response.read()

            except Exception as e:

                CTxtHelper.appendlog("[Error] post,errmsg=" + str(e))

            return ret

     

        # url = "http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa"

        @staticmethod

        def get(url):

            req = urllib2.Request(url)

            print req

            res_data = urllib2.urlopen(req)

            res = res_data.read()

            print res

    GET接口

    http://127.0.0.1:5000/test/

     

    POST接口

    http://127.0.0.1:5000/query/

    {"sql":"select * from tb_BoxError"}

     

    http://127.0.0.1:5000/execute/

    {"sql":"delete from tb_BoxError where fi_State=1 and fi_Time2 <='2018-10-10 23:59:59' and fi_DeviceID='8888'"}

     

  • 相关阅读:
    MySQL主从.md
    mysqldump.md
    MySQL管理.md
    SQL语句.md
    如何在同一台服务器上部署两个tomcat
    loadrunner函数解密之web_reg_save_param
    loadrunner函数解密之web_reg_find
    Jmeter如何保持cookie,让所有请求都能用同一个cookie,免去提取JSESSIONID
    Jmeter如何提取响应头部的JSESSIONID
    Loadrunner如何进行有效的IP欺骗
  • 原文地址:https://www.cnblogs.com/chen1880/p/11239265.html
Copyright © 2020-2023  润新知