Wednesday, 7 January 2015

Python web server

from pprint import pformat
from wsgiref.simple_server import make_server

def app(environ, start_response):
    headers = {'Content-Type': 'text/plain; charset=utf-8'}
    start_response('200 OK', list(headers.items()))
    #yield is used to send back response
    yield 'Here is the WSGI environment:\r\n\r\n'.encode('utf-8')
    yield pformat(environ).encode('utf-8')
    path = environ.get('PATH_INFO', '/')
    print path


if __name__ == '__main__':
    httpd = make_server('', 8000, app)
    host, port = httpd.socket.getsockname()
    print('Serving on', host, 'port', port)
    httpd.serve_forever()
python Server.py
run at browser as http://ip:8000/ ===>>> http://localhost:8000/

No comments:

Post a Comment