Advertisement
Guest User

Python simple HTTP server with specific GET handler

a guest
Apr 22nd, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. from BaseHTTPServer import HTTPServer
  2. from SimpleHTTPServer import SimpleHTTPRequestHandler
  3.  
  4. class MyHandler(SimpleHTTPRequestHandler):
  5.     def do_GET(self):
  6.         # for my URIs, handle like this
  7.         if self.path.startswith('/my/'):
  8.             self.send_response(200)
  9.             self.send_header('Content-type', 'text/html')
  10.             self.end_headers()
  11.             self.wfile.write('<html><head><title>Spam</title></head><body>Eggs</body></html>')
  12.         # for all other URIs, use default handler
  13.         else:
  14.             SimpleHTTPRequestHandler.do_GET(self)
  15.  
  16. httpd = HTTPServer(("", 8000), MyHandler)
  17. httpd.serve_forever()
  18.  
  19. # now call:
  20. #
  21. # http://localhost:8000
  22. # http://localhost:8000/file.txt
  23. # http://localhost:8000/my/anything
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement