Reputation: 454
I'm attempting to serve a flask REST API which using gevent wsgiserver. It worked in Werkzeug, but did not respond in gevent wsgiserver. Here is a simple flask app serve by gevent wsgiserver.
import gevent
from gevent.pywsgi import WSGIServer
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index_get():
return 'OK'
if __name__ == '__main__':
# app.run(host='0.0.0.0',port=80, threaded=True, debug=False)
WSGIServer(('0.0.0.0', 80), app).serve_forever()
Python-3.5.2
flask-0.11.1
gevent-1.2.1
The browser keep loading the response. However the server console show it already give the response. Accidentally, I change the code
@app.route('/', methods=['GET'])
def index_get():
return 'Worked'
And the browser receive the response successfully. I found it fail to respond only when response string is a one or two character string.
Can anyone confirmed this issue?
What is the mechanism in it?
How can I workaround so I can respond a two character string?
Upvotes: 2
Views: 1236
Reputation: 454
OK, I really should read the tutorial first.
According to the tutorial
The stream is closed when a size zero chunk is sent.
I should append a zero size chunk like '\n'
@app.route('/', methods=['GET'])
def index_get():
return json.dumps({'ReturnCode':200,'message':'command sent.'}) + '\n'
It appear gevent's WSGIServer is a little different from Werkzeug. I should make some change to suit it. The recommended application is different from flask. Although, just append a '\n'
also worked for me.
from gevent.wsgi import WSGIServer
def application(environ, start_response):
status = '200 OK'
body = '<p>Hello World</p>'
headers = [
('Content-Type', 'text/html')
]
start_response(status, headers)
return [body]
WSGIServer(('', 8000), application).serve_forever()
Upvotes: 2