Python server that stores visitors user agent
server.py
import os
from http.server import BaseHTTPRequestHandler, HTTPServer
class RequestHandler(BaseHTTPRequestHandler):
def _send_response(self, content):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes(content, "utf8"))
def do_GET(self):
if self.path == '/':
with open("index.html", 'r') as file:
content = file.read()
self._send_response(content)
with open("useragent.txt", "a") as f:
f.write(self.headers.get("User-Agent") + " | " + self.client_address[0] + "\n")
def run():
server_address = ('', 80)
httpd = HTTPServer(server_address, RequestHandler)
print("serving at port", 80)
httpd.serve_forever()
run()