Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created March 26, 2025 17:43
Show Gist options
  • Save untodesu/b1000eff4c4291772cbabc576af85b0c to your computer and use it in GitHub Desktop.
Save untodesu/b1000eff4c4291772cbabc576af85b0c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import datetime
import os, re
import uuid
import sys
log_file = open("server.log", "a", encoding="utf-8")
response_headers = []
response_body = []
def add_log(message):
log_file.write("[{}] {}\n".format(datetime.datetime.now().isoformat(), message))
log_file.flush()
def read_line():
line = sys.stdin.readline().rstrip()
add_log("stdin: {}".format(line if line else "<empty line>"))
return line
def write_line(line):
add_log("stdout: {}".format(line if line else "<empty line>"))
sys.stdout.write(line + "\n")
# Read the GET header
header_line = read_line()
header = re.search(r"\s*[Gg][Ee][Tt]\s+\/(.*)\s+[Hh][Tt][Tt][Pp]\/[0-9]+\.[0-9]+$", header_line)
# Read all other HTTP headers
while True:
if read_line():
continue
break
if header:
response_headers.append("HTTP/1.0 200 OK")
response_headers.append("Connection: close")
response_headers.append("Content-type: text/html")
response_body.append("<html><body>")
response_body.append("<h1>Have your heard of the popular hit game among us?")
response_body.append("&nbsp;It's a really cool game where 1-3 imposters try to kill")
response_body.append("&nbsp;off the crewmates, while the crew has to finish their")
response_body.append("&nbsp;tasks or vote off the imposters to win. It's 5 dollars")
response_body.append("&nbsp;on steam and other consoles but it is free on App Store and Google Play.</h1>")
response_body.append("<h2>You requested /{}</h2>".format(header.group(1)))
response_body.append("<pre>{}</pre><br/>".format(os.popen("uname -a").read()))
response_body.append("<code>");
for i in range(0, 100):
response_body.append("{} ".format(uuid.uuid4()))
response_body.append("</code><br/>")
response_body.append("<code>{}</code>".format(os.popen("ls /usr/bin").read()))
response_body.append("</body></html>")
else:
response_headers.append("HTTP/1.0 418 I'm a teapot")
response_headers.append("Connection: close")
response_headers.append("Content-type: text/html")
response_body.append("<html><body>")
response_body.append("<h1>HTTP 418: I'm a teapot</h1>")
response_body.append("<h2>Malformed HTTP header</h2>")
response_body.append("</body></html>")
full_response_body = "".join(response_body)
response_headers.append("Content-Length: {}".format(len(full_response_body)))
for header in response_headers:
write_line(header)
write_line("")
write_line(full_response_body)
write_line("")
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment