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

系统 206 0

源码下载 -> 提取码  QQ:505645074

 

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'"}

 

 


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请请扫描上面二维码支持博主1元、2元、5元等您想捐的金额吧,狠狠点击下面给点支持吧

发表我的评论
最新评论 总共0条评论