Skip to content

Instantly share code, notes, and snippets.

@shieldwed
Created October 14, 2017 21:01
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shieldwed/0312c39fa486912060c68bfd314f2393 to your computer and use it in GitHub Desktop.
Save shieldwed/0312c39fa486912060c68bfd314f2393 to your computer and use it in GitHub Desktop.
A simple python HTTP server which responds with the original request headers and request path
#!/usr/bin/env python
from http.server import BaseHTTPRequestHandler, HTTPServer
# HTTPRequestHandler class
class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
# GET
def do_GET(self):
# Send response status code
self.send_response(200)
# Send message back to client
message = bytes(
str(self.headers) +
"\n" +
self.requestline +
"\n"
, 'utf8')
# Send headers
self.send_header('Content-type','text/plain; charset=utf-8')
self.send_header('Content-length', str(len(message)))
self.end_headers()
# Write content as utf-8 data
self.wfile.write(message)
return
def run():
print('starting server...')
# Server settings
# Choose port 8080, for port 80, which is normally used for a http server, you need root access
server_address = ('127.0.0.1', 8081)
httpd = HTTPServer(server_address, testHTTPServer_RequestHandler)
print('running server...')
httpd.serve_forever()
run()
@cooliscool
Copy link

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment