onhuko 2023-02-03 23:40 采纳率: 56%
浏览 126
已结题

这段代码有什么bug

这段代码有什么bug
main.py


import requests, json
from flask import Flask, session, make_response, redirect, url_for

app = Flask(__name__)


with open('web/jpg/1.jpg', 'rb') as f:
    img = f.read()

result = requests.post('http://127.0.0.1:24401/', params={'threshold': 0.1},
                                                  data=img).json()

print(result["results"][0]["confidence"])
global traceback
# 全局化
traceback = result["results"][0]["confidence"]
# 变量赋值

#路径定义,方便单片机B接收
@app.route('/', methods=['GET', 'POST']) 
def data_handler(confidence) :
    r = make_response('

Accomplished

'
) r.set_cookie('confidence', traceback) return json.dumps({'confidence': traceback}) #转化为Python字典 app.run(port= 80, debug=True) #port端口号 默认于127.0.0.1运行

client.py

import threading
import urllib.request, requests
handler_url = 'http://......'
#此处为main.py中的ip地址+main.py中/data/api/handle路径

class check_data(threading.Thread) :
        def __init__(self, confidence) :
                threading.Thread.__init__(self)
                self.event = threading.Event()
                self.confidence =  confidence

        def run(self) :
                global handler_url

                try :
                        while not self.event.is_set() :
                                r = requests.get(handler_url)
                                Specimen = r.cookies['confidence']
                                print(Specimen)
                except :
                       pass
                self.event.wait(1)
                #()可以更改即更改间隔时间

  • 写回答

4条回答 默认 最新

  • 社区专家-Monster-XH 2023-02-03 23:51
    关注

    你应该是想发送照片到端口,获取cookie,打印,启动web,接受结果。

    两个代码修改成同一文件:

    
    import requests, json, threading
    from flask import Flask, session, make_response, redirect, url_for
     
    app = Flask(__name__)
    handler_url = 'http://......'
     
    with open('web/jpg/1.jpg', 'rb') as f:
        img = f.read()
     
    result = requests.post('http://127.0.0.1:24401/', params={'threshold': 0.1}, data=img).json()
     
    print(result["results"][0]["confidence"])
    global traceback
    # 全局化
    traceback = result["results"][0]["confidence"]
    # 变量赋值
     
    #路径定义,方便单片机B接收
    @app.route('/', methods=['GET', 'POST']) 
    def data_handler():
        r = make_response('Accomplished')
        r.set_cookie('confidence', traceback)
        return json.dumps({'confidence': traceback})
        #转化为Python字典
     
    class check_data(threading.Thread) :
            def __init__(self):
                    threading.Thread.__init__(self)
                    self.event = threading.Event()
     
            def run(self) :
                    global handler_url
     
                    try :
                            while not self.event.is_set() :
                                    r = requests.get(handler_url)
                                    Specimen = r.cookies['confidence']
                                    print(Specimen)
                    except :
                           pass
                    self.event.wait(1)
                    #()可以更改即更改间隔时间
     
    app.run(port= 80, debug=True)
    #port端口号 默认于127.0.0.1运行
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 2月12日
  • 已采纳回答 2月4日
  • 创建了问题 2月3日