dummy-web-server.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python2
  2. """
  3. Very simple HTTP server in python.
  4. Downloaded from: https://gist.github.com/bradmontgomery/2219997
  5. Usage::
  6. ./dummy-web-server.py [<port>]
  7. Send a GET request::
  8. curl http://localhost
  9. Send a HEAD request::
  10. curl -I http://localhost
  11. Send a POST request::
  12. curl -d "foo=bar&bin=baz" http://localhost
  13. """
  14. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  15. import SocketServer
  16. class S(BaseHTTPRequestHandler):
  17. def _set_headers(self):
  18. self.send_response(200)
  19. self.send_header('Content-type', 'text/html')
  20. self.end_headers()
  21. def do_GET(self):
  22. self._set_headers()
  23. self.wfile.write("<html><body><h1>hi!</h1></body></html>")
  24. def do_HEAD(self):
  25. self._set_headers()
  26. def do_POST(self):
  27. # Doesn't do anything with posted data
  28. self._set_headers()
  29. self.wfile.write("<html><body><h1>POST!</h1></body></html>")
  30. def run(server_class=HTTPServer, handler_class=S, port=80):
  31. server_address = ('', port)
  32. httpd = server_class(server_address, handler_class)
  33. print 'Starting httpd...'
  34. httpd.serve_forever()
  35. if __name__ == "__main__":
  36. from sys import argv
  37. if len(argv) == 2:
  38. run(port=int(argv[1]))
  39. else:
  40. run()