I am debugging a microcontroller I've built which is writing raw HTTP requests line by line. I am using Flask for my backend and I would like to see the entire request as it appears in this format:
GET / HTTP/1.1
Content-length: 123
User-agent: blah
...
I know Flask is based on WSGI. Is there anyway to get this to work with Flask?
Why not?
I've used the headers but you can use the same aproach to print any request attribute. The docs are here: http://flask.pocoo.org/docs/0.12/api/#flask.Request.
Also you need to setup FLASK_DEBUG=1 to Flask.logger.debug to work, what is nice since you can disable it in production.
Regards,
suppose if you want complete details,
There is an another way
This doesn't use flask but it is fairly simple to setup a socket echo server.
With flask you have access to the request object which contains all the HTTP details:
Yes, Flask is a WSGI application, so it is trivial to wrap your app in an extra layer that logs the request:
This defines a piece of middleware to wrap your Flask application in. The advantage is that it operates entirely independent of Flask, giving you unfiltered insight into what goes in and what comes out.
How you apply the middleware depends on the exact WSGI server you are using; see your WSGI server documentation.
When running Flask with the built-in server (
app.run()
), do:The little
app.wsgi_app
wrapping dance places theLoggingMiddleware
around the Flask WSGI application.The output goes to the
wsgi.error
stream; where that ends up again depends on your WSGI server;mod_wsgi
puts this in the Apache error log for your site, the bundled Flask server prints this tostderr
.