SHARE
TWEET

Python simple HTTP server with specific GET handler

a guest Apr 22nd, 2016 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top