I have a Flask application that runs with Flask-SocketIO. I recently installed eventlet in order to improve performance and utilise the web socket protocol.
My HTTP logs started having 2 additional parameters at the end (after the status code):
127.0.0.1 - - [26/Sep/2019 15:27:58] "GET /supported_countries HTTP/1.1" 200 488 0.019999
127.0.0.1 - - [26/Sep/2019 15:27:58] "GET /specializations HTTP/1.1" 200 381 0.003003
In this case it's the numbers 488 0.019999 and 381 0.003003.
I am assuming it's the size of the response and the time it took to complete the request?
What are they? (and can I configure what request info is logged?)
Here is my application.py
from my_app import create_app, socketio
app = create_app()
if __name__ == '__main__':
socketio.run(app, host=app.config.get('APP_HOST'),
log_output=app.config.get('LOGGING', False))
Again, please note that this was not happening prior to the installaiton of eventlet. Flask-SocketIO atuomatically detects that I have it installed and selects it (emphasis mine):
The extension automatically detects which asynchronous framework to use based on what is installed. Preference is given to eventlet, followed by gevent. For WebSocket support in gevent, uWSGI is preferred, followed by gevent-websocket. If neither eventlet nor gevent are installed, then the Flask development server is used.
So the Flask dev server does not output these numbers, whilst the eventlet-configured server does.
Related
I'm working in PyCharm and building endpoints with Flask, and I use Pyctuator as well. It is connected to a Spring Boot Admin Server, and I saw a lot of logs, which was unuseful for me, like:
2022-07-26 10:28:32,212 INFO 1 -- [Thread-17317] _internal: 172.18.0.1 - - [26/Jul/2022 10:28:32] "GET /actuator/loggers HTTP/1.1" 200 -
I turned off the loggers on the site, and I want to setup in PyCharm if the server do any processes with any endpoints, then send back messages, like: 'System started xy process', 'System stopped xy process.' etc.
Could you please help how can I set it up in PyCharm?
Thanks!
Okay so I've realized that something weird is happening when I try to upload more than I specified in my config.py which is:
class Config:
#other configurations
MAX_CONTENT_LENGTH = 10 * 1024 * 1024
When I try to upload more than 10 MB, instead of giving me a 413 Error, application just refuses to connect. My error handler:
#errors.app_errorhandler(413)
def error_413(error):
return render_template('errors/413.html'), 413
My run.py:
from flaskblog import create_app
app = create_app()
if __name__ == "__main__":
app.run(debug=True)
I can see on terminal that I've gotten this error:
"POST /foo/bar HTTP/1.1" 413 -
Although my app seems to be running on the terminal, I can't access it whatsoever. It's just dead on the browser:
ERR_CONNECTION_REFUSED
I tried running it on uWSGI, Werkzeug, other browsers, no luck.
Any idea what's happening?
EDIT: I can access after I restart my computer. But I'm still curious why about this happens.
Also I use Cloud SQL with external IP for more information.
I had a similar error when using the MAX_CONTENT_LENGTH configuration.Did you register the blueprint errors if yes,Please try removing app_ from #errors.app_errorhandler.Like below:
#errors.errorhandler(413)
def error_413(error):
return render_template('errors/413.html'), 413
If this didn't work try removing the MAX_CONTENT_LENGTH line or if you are trying to connect from another device, run your app with the following command flask run --host=0.0.0.0 and then access it on the other device using your router's ip address which usually looks like 192.168.xxx.xxx and the port which your app is running on like 192.168.xxx.xxx:<port_of_your_app>.If this doesn't work it might be an issue with your firewall refusing incoming connections in the port that your app is running on in which case you can run the following command on your terminal sudo ufw allow <YOUR_PORT>.
Okay next time I should read documentations more carefully:
Connection Reset Issue When using the local development server, you
may get a connection reset error instead of a 413 response. You will
get the correct status response when running the app with a production
WSGI server.
from Flask - File uploads.
I have a default "Hello World" flask app running on my Windows 10 machine. Here is the code I'm using:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello_world():
return "Hello, World!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
When I access it from another computer on my network (My phone in this case) I get Hello, World! in the browser, and the flask app outputs <my phone ip> - - [25/Feb/2020 15:55:14] "GET / HTTP/1.1" 200 - to the console.
The weird part is that when I try to access localhost:5000 on the computer running the app, the browser hangs, and the flask app outputs 127.0.0.1 - - [25/Feb/2020 15:57:12] "GET / HTTP/1.1" 200 - to the console. It will hang forever.
Do you think this is a problem with my network configuration? I've had weird things happen in the past as a result of having Wireshark installed, Hyper-V, etc.? I've already disabled all virtual adapters other than my wifi.
Edit 1:
If I open another python interpreter I can use requests and it gives me b'Hello, World!' as the response content. Both browsers I've tried, Chrome and MS Edge, hang.
Edit 2:
For now I'm just going to resort to using Postman, since I'm using this to design an API anyway. I'm just confused why this is happening 🤔
Is windows firewall on? The code looks fine. This a network issue. Turn your firewall off to test.
The issue should be reproducible with the following two minimal examples:
Minimal example with app.run()
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello'
app.run()
Minimal example with gevent.pywsgi.WSGIServer
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello'
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
server = pywsgi.WSGIServer(('127.0.0.1', 5000), app, handler_class=WebSocketHandler)
server.serve_forever()
The first 5 lines are identical, so both examples only differ in the way they start the server. Both servers do work, I get "Hello" in the browser. The first example prints:
* Serving Flask app "1" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [20/Jun/2019 23:43:15] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [20/Jun/2019 23:43:19] "GET / HTTP/1.1" 200 -
So the console output shows some information about every request which is handled by the server.
However, with the second example, I don't get any logging output in the console anymore. How do I enable logging for gevent.pywsgi.WSGIServer and WebSocketHandler?
Background (which doesn't matter regarding the issue, I think)
I'm running a Flask app which uses flask_sockets. Because
Werkzeug development server cannot provide the WSGI environ with a websocket interface
I am not able to use the server with app.run() and I'm using gevent.pywsgi.WSGIServer instead. The code of my example above is taken directly from the module's examples at https://github.com/heroku-python/flask-sockets without any modifications.
Logging works differently with gevent.pywsgi.WSGIServer. It uses python logging and is much more sophisticated and flexible.
Here's an example:
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
import logging
logging.basicConfig(level=logging.INFO)
server = pywsgi.WSGIServer(('127.0.0.1', 5000), app, handler_class=WebSocketHandler)
server.serve_forever()
And now wherever you want output, you can do:
logging.info("You can see me now...")
If you want to see startup info from WSGIServer, then set the log level to DEBUG and you can see tons of output.
It should work like that, but it's a bug in WebSocketHandler from the geventwebsocket module which has already been reported: https://gitlab.com/noppo/gevent-websocket/issues/16
I ran the following Flask app and tried to post to /, but I got a 404 response. The / route is defined, why doesn't it get handled?
from flask import Flask, request
app = Flask(__name__)
app.run()
#app.route('/', methods=['POST'])
def handle_post_request():
return str(request.form)
C:\Python35\python.exe D:/Dropbox/dev/contact.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [03/Apr/2017 20:46:01] "POST / HTTP/1.1" 404 -
Calling app.run blocks, no code is executed after it until the server is stopped. Make sure your app is completely set up before calling run. In this case, move the route definition above the call to run.
Flask 0.11 introduced a more robust way to start applications to avoid this and other common mistakes. Completely remove app.run(). Set the FLASK_APP environment variable to your app's entry point, and call flask run.
FLASK_APP=contact.py flask run
You can also set FLASK_DEBUG=1 to enable debug mode.
In Windows, use set FLASK_APP=contact.py to set environment variables instead.