python WSGI 接口

from wsgiref.simple_server import make_server
import json
# 导入我们自己编写的application函数:
def geturl(datas,name):
    arr = []
    for i in datas:
        arr.append(str(i)[:int(i.index("="))])
        arr.append(str(i)[int(i.index("=")) + 1:])

    print(arr)
    getdata = {}
    for i in range(0, len(arr), 2):
        getdata[arr[i]] = arr[i + 1]

    return getdata[name]
def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/json;charset=utf-8')])
    url = environ['PATH_INFO'][1:]
    datas = str(environ['QUERY_STRING']).split('&')
    if url =="":
        req="server ok"
    if url =="api":

        # print(datas)
        # # datas = str(environ['QUERY_STRING'])

        r= geturl(datas,'u')
        r1 = geturl(datas, 'y')
        data = {

            'code': 1,
            'msg': "公司",
            'data':r,
            'data2': r1

        }
        req= json.dumps(data,ensure_ascii=False)

    return [req.encode()]
# 创建一个服务器,IP地址为空,端口是8000,处理函数是application:
httpd = make_server('', 8000, application)
print('Serving HTTP on port 8000...')
# 开始监听HTTP请求:
httpd.serve_forever()

评论

刷新

友情链接