发帖
充值
 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
首页源码分享使用python_Flask框架_懒人精灵双端信息通信 ...

使用python_Flask框架_懒人精灵双端信息通信

5
回复
4146
查看
[ 复制链接 ]
已绑定手机

46

主题

118

回帖

2831

积分

版主

积分
2831
2021-11-27 10:52:09 显示全部楼层 阅读模式
使用python,flask框架实现懒人精灵APP双端信息通信.

懒人精灵交互.py 文件源码:
  1. import json
  2. import os
  3. from flask import Flask, request, make_response,send_from_directory
  4. app = Flask(__name__)
  5. send_dict = {"Aitlo":[]}
  6. @app.route('/create/accept', methods=["GET", "POST"])
  7. def create_accept():
  8.     global send_dict
  9.     new_send = {}
  10.     if request.form.get("userID") in send_dict and len(send_dict[request.form.get("userID")]) != 0:
  11.         print("找到了对应的ID,发送数据")
  12.         new_send["serverseedstr"] = send_dict[request.form.get("userID")][-1]
  13.         del send_dict[request.form.get("userID")][-1]
  14.     return json.dumps(new_send)

  15. @app.route('/create/send', methods=["GET", "POST"])
  16. def create_send():
  17.     # 接收代签名的字符串
  18.     new_dict = {}
  19.     # print("接收客户端发送的数据:",new_dict)
  20.     clientstr = request.form.get("sendstr")
  21.     send_dict[request.form.get("userID")].append(clientstr)
  22.     ip = request.remote_addr
  23.     new_dict["userIP"] = ip
  24.     new_dict["state"] = True
  25.     return str(new_dict)

  26. if __name__ == '__main__':
  27.     # app.run()
  28.     app.run(host="0.0.0.0", port="5000", debug=True)
复制代码


懒人精灵交互.lua 文件源码:
  1. function AcceptData(url,tab)
  2.     -- 客户端发请求接收服务端返回数据
  3.     local ret1,code = httpPost(string.format("%s/create/accept",url),string.format("userID=%s",tab["userID"]))
  4.     local ret = jsonLib.decode(ret1)
  5.     print(ret)
  6.     if ret['serverseedstr'] ~= nil then
  7.         print(ret['serverseedstr'])
  8.         toast(ret['serverseedstr'],0,0,12)
  9.         sleep(3000)
  10.     end
  11.     return ret1
  12. end


  13. function SendData(url,tab)
  14.     -- 客户端发送数据至服务器
  15.     print(tab)
  16.     local strdata = {}
  17.     local ret1,code = httpPost(string.format("%s/create/send",url),string.format("userID=%s&sendstr=%s",tab["userID"],tab["sendstr"]))
  18.     print(ret1)
  19.     return ret1
  20.    
  21. end

  22. tab = {userID="Aitlo",sendstr="测试数据发送成功!"}

  23. url = "http://127.0.0.1:5000"
  24. -- 发送消息至服务器

  25. -- SendData(url,tab)

  26. -- 客户端发送请求至服务器拿数据
  27. -- AcceptData(url,tab)

  28. -- 每隔一秒钟香服务端发送请求拿数据.
  29. --[===[while 1 do
  30. sleep(1000)
  31. AcceptData(tab)
  32. end]===]
复制代码
运行结果图片:
1.png QQ截图20211127104753.png

使用道具
举报

2

主题

13

回帖

99

积分

注册会员

积分
99
6666666666666
回复
使用道具
举报

4

主题

17

回帖

77

积分

注册会员

积分
77
懒人懒人懒人懒人懒人懒人懒人懒人懒人懒人
回复
使用道具
举报
已绑定手机

3

主题

40

回帖

3454

积分

论坛元老

积分
3454
楼主厉害啊
回复
使用道具
举报

0

主题

15

回帖

40

积分

新手上路

积分
40

谢谢大佬分享 !
回复
使用道具
举报

2

主题

40

回帖

112

积分

注册会员

积分
112
感谢楼主分享
回复
使用道具
举报
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则