我正在使用Flask和Python 3.7。 我已经实现了一个像这样的hello world应用程序:
from wsgiref.simple_server import WSGIServer
from flask import request, json
from base.flask_instance import FlaskInstance
app = FlaskInstance.get_instance()
@app.route('/hello', methods=['GET'])
def _signup():
try:
return "hello world"
except BaseException as e:
print(e)
if __name__ == '__main__':
# for using in development server.
app.run(debug=True, host='0.0.0.0', port=5000)
# for using in production server.
http_server = WSGIServer(server_address=('', 5000), RequestHandlerClass=app)
http_server.serve_forever()
当我使用此代码段运行它时,它工作正常:
app.run(debug=True, host='0.0.0.0', port=5000)
但是此代码段适用于开发服务器,因此不应在生产部署中使用。因此,我正在使用此代码段运行应用程序:
http_server = WSGIServer(server_address=('', 5000), RequestHandlerClass=app)
http_server.serve_forever()
此代码段也运行良好,但是在调用post请求后,它将引发此异常:
Exception happened during processing of request from ('192.168.1.13', 1978)
Traceback (most recent call last):
File "C:\Users\milad\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 316, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Users\milad\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 347, in process_request
self.finish_request(request, client_address)
File "C:\Users\milad\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
TypeError: __call__() takes 3 positional arguments but 4 were given
如果我通过Python 2.7运行此应用程序,它将运行得像个魅力! 我应该怎么做才能用Python 3.7运行它?
答案 0 :(得分:1)
# from wsgiref.simple_server import WSGIServer
from gevent.pywsgi import WSGIServer
from flask import request, json, Flask
# from base.flask_instance import FlaskInstance
#
# app = FlaskInstance.get_instance()
app = Flask(__name__)
@app.route('/hello', methods=['GET'])
def _signup():
try:
return "hello world"
except BaseException as e:
print(e)
if __name__ == '__main__':
# for using in development server.
# app.run(debug=True, host='0.0.0.0', port=5000)
# for using in production server.
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
您应该从“ gevent.pywsgi”导入“ WSGIServer”,并且WSGIServer()的输入类型已更改