Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Python
Posted by: Peter Tsiampas
Added: Apr 15, 2018 12:19 PM
Views: 20
Tags: webservice
  1. from codecs import latin_1_encode
  2. from wsgiref.simple_server import make_server
  3.  
  4. def my_handler(environ, start_response):
  5.     path_info = environ.get("PATH_INFO", None)
  6.     query_string = environ.get("QUERY_STRING", None)
  7.     response_body = "You asked for {0} with query {1}".format(
  8.                                           path_info, query_string)
  9.     response_headers = [("Content-Type", "text/plain"),
  10.                ("Content-Length", str(len(response_body)))]
  11.     start_response("200 OK", response_headers)
  12.     response = latin_1_encode(response_body)[0]
  13.     return [response]
  14.  
  15. httpd = make_server("127.0.0.1",  8000,  my_handler)
  16. httpd.serve_forever()   # Start the server listening for requests
  17.