Last active
March 11, 2016 07:47
-
-
Save svanellewee/4babb24c4bc2e666929c to your computer and use it in GitHub Desktop.
WSGI example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| uwsgi --http :8000 --wsgi-file simpleapp.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # curl -X POST -d '{"hello":"world"}' -v localhost:8000 -H "Content-Type: application/json" | |
| import json | |
| def application(environ, start_response): # <--- uwsgi expects this | |
| status = '200 OK' | |
| response_headers = [('Content-type', 'text/plain')] | |
| start_response(status, response_headers) | |
| for i,v in sorted(environ.items()): | |
| print(i,v) | |
| content_length = int(environ.get('CONTENT_LENGTH',0)) | |
| content = environ['wsgi.input'].read(content_length) | |
| print("CONTENT = {}:{}".format(type(content),content_length)) | |
| print(json.loads(content)) | |
| return ['Hello world\n'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from wsgiref.util import setup_testing_defaults | |
| from wsgiref.simple_server import make_server | |
| from simple_app import application | |
| httpd = make_server('', 8000, application) | |
| print "Serving on port 8000..." | |
| httpd.serve_forever() |
Author
Author
2 ways to configure wsgi based servers (internal python wsgi OR uwsgi (c-implemented)
Author
you can actually pip install uwsgi as well!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WSGI = give it something to call and get an iterable back.
You could also implement above with a class that implements an iter function instead of it being callable:
Effect is the same as the above function .