Search code examples
pythonflaskwsgipython-3.7wsgiserver

WSGIServer TypeError: __call__() takes 3 positional arguments but 4 were given


I'm using Flask and Python 3.7. I've implemented a hello world app like this:

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()

It works fine when I use this snippet to run it:

app.run(debug=True, host='0.0.0.0', port=5000)

But this snippet is for development server and it should not be used in a production deployment. So I'm using this snippet for run app:

http_server = WSGIServer(server_address=('', 5000), RequestHandlerClass=app)
http_server.serve_forever()

This snippet runs well too But after calling post request, it throws this exception:

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

If I run this app by Python 2.7, It runs like a charm! What should I do to run it by Python 3.7?


Solution

  • # 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()
    
    

    you should import 'WSGIServer' from 'gevent.pywsgi' and input types of WSGIServer() are changed