Skip to content

Instantly share code, notes, and snippets.

@jxxcarlson
Last active March 29, 2020 10:17
Show Gist options
  • Save jxxcarlson/5a590691b17c185e4124644304f2eeb8 to your computer and use it in GitHub Desktop.
Save jxxcarlson/5a590691b17c185e4124644304f2eeb8 to your computer and use it in GitHub Desktop.
Get hardware generated random number via HTTP. Uses /dev/random
#!/usr/bin/env python3
"""
I have hacked the code at
https://gist.github.com/bradmontgomery/2219997#file-dummy-web-server-py
to make a server that responds to get requests by returning
a random integer generated using the /dev/random hardware
random number generator. Assuming that this file is named
"server.py", just do
python3 server.py -l 0.0.0.0 -p 8000
To test the server locally, do
curl http://localhost:8000
If the local IP address of the machine you are running the server
on is 123.456.7.8, just paste this number into the browser
of any machine on your local net, and you will get a random
number.
Silly, of course, but so much fun!
--------------------------------------------------------
Very simple HTTP server in python (Updated for Python 3.7)
Usage:
./dummy-web-server.py -h
./dummy-web-server.py -l localhost -p 8000
Send a GET request:
curl http://localhost:8000
Send a HEAD request:
curl -I http://localhost:8000
Send a POST request:
curl -d "foo=bar&bin=baz" http://localhost:8000
"""
import argparse
from http.server import HTTPServer, BaseHTTPRequestHandler
import os
from base64 import b64decode
# cmd = "od -vAn -N4 -tu4 < /dev/urandom"
# cmd = "head -c 112 /dev/random"
cmd = "head -c 8 /dev/random | base64"
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def encode_text(self, message):
content = message
return content.encode("utf8") # NOTE: must return a bytes object!
def do_GET(self):
self._set_headers()
random_string = os.popen(cmd).read()
random_integer = int.from_bytes(b64decode(random_string), 'big')
print(random_integer)
self.wfile.write(self.encode_text(str(random_integer) + "\n"))
def do_HEAD(self):
self._set_headers()
def do_POST(self):
# Doesn't do anything with posted data
self._set_headers()
self.wfile.write(self._html("POST!"))
def run(server_class=HTTPServer, handler_class=S, addr="0.0.0.0", port=8000): # 0.0.0.0 instead of localhost
server_address = (addr, port)
httpd = server_class(server_address, handler_class)
print(f"Starting httpd server on {addr}:{port}")
httpd.serve_forever()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run a simple HTTP server")
parser.add_argument(
"-l",
"--listen",
default="localhost",
help="Specify the IP address on which the server listens",
)
parser.add_argument(
"-p",
"--port",
type=int,
default=8000,
help="Specify the port on which the server listens",
)
args = parser.parse_args()
run(addr=args.listen, port=args.port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment