Thursday, July 23, 2009

Python WSGI simple example

# WSGI is the "Web Server Gateway Interface"
# It is a protocol for web servers.
# This example provides the easy_app function as the wsgi component
# When you run this example it will create a simple web server and you'll
# be able to browse to your wsgi web site at http://localhost:8000/
# To expand and improve this example take a gander at the wsgi site
# Python version >=2.5 should be able to run this example without
# additional libraries. Everything is included with python!


def easy_app(environ, start_response):
"""Easiest wsgi implementation ever!"""
status = '200 OK'
response_headers = [('Content-type','text/plain')]
start_response(status, response_headers)
return ['Python and WSGI Rules!\n']


from wsgiref.simple_server import make_server

httpd = make_server('', 8000, easy_app)

# reminder of where the website is being served
print("Browse to: http://localhost:8000/")

# run server for ever (you'll need to kill it to stop it)
httpd.serve_forever()

No comments:

Post a Comment