gpt4 book ai didi

javascript - Flask + Ajax 集成 : AttributeError: 'WSGIRequestHandler' object has no attribute 'environ'

转载 作者:行者123 更新时间:2023-11-27 23:11:35 28 4
gpt4 key购买 nike

所以我尝试使用ajax来执行一个python文件,该文件基本上连续检查文本文件内容是否正在更改,如果是,它将返回到ajax,在那里它将执行适当的操作。 python 脚本需要在页面加载时开始。

这是我的 python 文件:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def checkFile():
filename = "demo.txt"
while True:
file = open(filename)
for line in file:
value = line
intValue = int(value)
if intValue < 50:
return "done"
else:
continue

if __name__ == "__main__":
app.run(host='127.0.0.1', debug=True, port=5000)

这是我的 JS 文件(在 read() 内):

            $.ajax({
type:'GET',
url: "https://localhost:5000",
dataType: "text",
success: function(data) {
console.log("Response-Success: " + data);
if(data == "done") {
console.log("DONE!!!");
}
else {
console.log("NOPE!!!");
}
},
error:function(data) {
console.log("Response-Error: " + data);
}
}).done(function(data) {
console.log("Response-Done: " + data);
});

我收到的错误是:(这是对 python 脚本的请求发生时,这显示在 python 服务器控制台中):

Exception happened during processing of request from ('127.0.0.1', 52078)
Traceback (most recent call last):
File "C:\Python27\lib\SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\lib\SocketServer.py", line 655, in __init__
self.handle()
File "C:\Python27\lib\site-packages\werkzeug-0.11.4- py2.7.egg\werkzeug\serving .py", line 216, in handle
rv = BaseHTTPRequestHandler.handle(self)
File "C:\Python27\lib\BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "C:\Python27\lib\site-packages\werkzeug-0.11.4- py2.7.egg\werkzeug\serving.py", line 250, in handle_one_request
elif self.parse_request():
File "C:\Python27\lib\BaseHTTPServer.py", line 253, in parse_request
self.send_error(400, "Bad request version (%r)" % version)
File "C:\Python27\lib\BaseHTTPServer.py", line 364, in send_error
self.log_error("code %d, message %s", code, message)
File "C:\Python27\lib\site-packages\werkzeug-0.11.4-py2.7.egg\werkzeug\serving.py", line 272, in log_error
self.log('error', *args)
File "C:\Python27\lib\site-packages\werkzeug-0.11.4-py2.7.egg\werkzeug\serving.py", line 278, in log
_log(type, '%s - - [%s] %s\n' % (self.address_string(),
File "C:\Python27\lib\site-packages\werkzeug-0.11.4-py2.7.egg\werkzeug\serving.py", line 266, in address_string
return self.environ['REMOTE_ADDR']
AttributeError: 'WSGIRequestHandler' object has no attribute 'environ'

您能告诉我遇到这种情况该怎么办吗?谢谢。

最佳答案

我认为该错误可能是由于您的 javascript 文件中使用了“https”而引起的。

url: "https://localhost:5000"

我假设您在本地运行 Flask,并且它配置为仅处理 http。

关于javascript - Flask + Ajax 集成 : AttributeError: 'WSGIRequestHandler' object has no attribute 'environ' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36149617/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com