Skip to content

Instantly share code, notes, and snippets.

@svanellewee
Last active March 11, 2016 07:47
Show Gist options
  • Select an option

  • Save svanellewee/4babb24c4bc2e666929c to your computer and use it in GitHub Desktop.

Select an option

Save svanellewee/4babb24c4bc2e666929c to your computer and use it in GitHub Desktop.
WSGI example
uwsgi --http :8000 --wsgi-file simpleapp.py
# 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']
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()
@svanellewee

Copy link
Copy Markdown
Author

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:

class SimplApp(object):
     def __init__ (self,environ, start_response):
              self.environ, self.start_response = environ, start_response
     def __iter__(self):
               ...
               yield ...

Effect is the same as the above function .

@svanellewee

Copy link
Copy Markdown
Author

2 ways to configure wsgi based servers (internal python wsgi OR uwsgi (c-implemented)

@svanellewee

Copy link
Copy Markdown
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