使用python,flask框架实现懒人精灵APP双端信息通信.
懒人精灵交互.py 文件源码:
- import json
- import os
- from flask import Flask, request, make_response,send_from_directory
- app = Flask(__name__)
- send_dict = {"Aitlo":[]}
- @app.route('/create/accept', methods=["GET", "POST"])
- def create_accept():
- global send_dict
- new_send = {}
- if request.form.get("userID") in send_dict and len(send_dict[request.form.get("userID")]) != 0:
- print("找到了对应的ID,发送数据")
- new_send["serverseedstr"] = send_dict[request.form.get("userID")][-1]
- del send_dict[request.form.get("userID")][-1]
- return json.dumps(new_send)
- @app.route('/create/send', methods=["GET", "POST"])
- def create_send():
- # 接收代签名的字符串
- new_dict = {}
- # print("接收客户端发送的数据:",new_dict)
- clientstr = request.form.get("sendstr")
- send_dict[request.form.get("userID")].append(clientstr)
- ip = request.remote_addr
- new_dict["userIP"] = ip
- new_dict["state"] = True
- return str(new_dict)
- if __name__ == '__main__':
- # app.run()
- app.run(host="0.0.0.0", port="5000", debug=True)
复制代码
懒人精灵交互.lua 文件源码:
- function AcceptData(url,tab)
- -- 客户端发请求接收服务端返回数据
- local ret1,code = httpPost(string.format("%s/create/accept",url),string.format("userID=%s",tab["userID"]))
- local ret = jsonLib.decode(ret1)
- print(ret)
- if ret['serverseedstr'] ~= nil then
- print(ret['serverseedstr'])
- toast(ret['serverseedstr'],0,0,12)
- sleep(3000)
- end
- return ret1
- end
- function SendData(url,tab)
- -- 客户端发送数据至服务器
- print(tab)
- local strdata = {}
- local ret1,code = httpPost(string.format("%s/create/send",url),string.format("userID=%s&sendstr=%s",tab["userID"],tab["sendstr"]))
- print(ret1)
- return ret1
-
- end
- tab = {userID="Aitlo",sendstr="测试数据发送成功!"}
- url = "http://127.0.0.1:5000"
- -- 发送消息至服务器
- -- SendData(url,tab)
- -- 客户端发送请求至服务器拿数据
- -- AcceptData(url,tab)
- -- 每隔一秒钟香服务端发送请求拿数据.
- --[===[while 1 do
- sleep(1000)
- AcceptData(tab)
- end]===]
复制代码 运行结果图片:
|