zoukankan      html  css  js  c++  java
  • 物联网通信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'"}

     

  • 相关阅读:
    在宏块级冗余可调的多描述视频编码算法方面取得进展(转载)
    C++单例模式
    C++手动实现库函数
    C#操作java平台生成的公钥
    C++之位操作符
    Windows 8 应用栏
    Windows 8 本地数据存储
    Win8的页面缓存
    操作符sizeof
    three.js结合geoJson绘制中国地图
  • 原文地址:https://www.cnblogs.com/chen1880/p/11239265.html
Copyright © 2011-2022 走看看