1

我正在使用 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 运行它?

4

1 回答 1

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() 的输入类型被改变

于 2019-08-03T07:53:32.433 回答