Skip to content

Instantly share code, notes, and snippets.

@alexlouden
Last active January 23, 2018 09:01
Show Gist options
  • Save alexlouden/009c5f058c35ec4d754542df6b586395 to your computer and use it in GitHub Desktop.
Save alexlouden/009c5f058c35ec4d754542df6b586395 to your computer and use it in GitHub Desktop.
Development HTTP/HTTPs servers to echo (and drop) incoming requests

Development HTTP and HTTPS servers to echo (and drop) incoming requests

Generate an SSL cert for HTTPS server

  1. Install openssl and python 3
openssl req -x509 -sha256 -nodes -newkey rsa:2048 -days 365 -keyout localhost.key -out localhost.crt -subj '/CN=localhost'
  1. Trust it (OS X)
sudo security add-trusted-cert -p ssl -d -r trustRoot -k ~/Library/Keychains/login.keychain localhost.crt

Run servers

HTTP - port 8080

$ python echoserver_http.py
Listening on port 8080...

HTTPS - port 8443

$ python echoserver_https.py
Listening on port 8443...

Demo

CURL:

curl 'https://localhost:8443/test' -X POST -H 'My-Header: Testing'
curl 'http://localhost:8443/test' -X POST -H 'My-Header: Testing'

HTTPS:

❯ python echoserver_https.py
Listening on port 8443...
HTTPServerRequest(protocol='https', host='localhost:8443', method='POST', uri='/test', version='HTTP/1.1', remote_ip='::1', headers={'My-Header': 'Testing', 'Host': 'localhost:8443', 'Accept': '*/*', 'User-Agent': 'curl/7.43.0'})

HTTP:

❯ python echoserver_http.py
Listening on port 8080...
HTTPServerRequest(protocol='http', host='localhost:8080', method='POST', uri='/test', version='HTTP/1.1', remote_ip='::1', headers={'My-Header': 'Testing', 'Host': 'localhost:8080', 'Accept': '*/*', 'User-Agent': 'curl/7.43.0'})
#!/usr/bin/env python
import tornado.ioloop
import tornado.web
import pprint
class MyDumpHandler(tornado.web.RequestHandler):
def post(self):
pprint.pprint(self.request)
pprint.pprint(self.request.body)
if __name__ == "__main__":
tornado.web.Application([(r"/.*", MyDumpHandler), ]).listen(8080)
print('Listening on port 8080...')
tornado.ioloop.IOLoop.instance().start()
#!/usr/bin/env python
import os
import ssl
import tornado.ioloop
import tornado.httpserver
import tornado.web
import pprint
class ReqHandler(tornado.web.RequestHandler):
def post(self):
pprint.pprint(self.request)
pprint.pprint(self.request.body)
def get(self):
pprint.pprint(self.request)
def options(self):
pprint.pprint(self.request)
if __name__ == "__main__":
application = tornado.web.Application([
(r'/.*', ReqHandler),
])
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_ctx.load_cert_chain(
os.path.abspath("./localhost.crt"),
os.path.abspath("./localhost.key")
)
http_server = tornado.httpserver.HTTPServer(application, ssl_options=ssl_ctx)
http_server.listen(8443)
print('Listening on port 8443...')
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment