snippets/server.py

47 lines
1.1 KiB
Python

#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
from hashlib import sha1
from pathlib import Path
class Server(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self._set_response()
self.wfile.write(f"GET request for {self.path}".encode())
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
hash = sha1()
hash.update(post_data)
post_path = Path(f'{hash.hexdigest()}.zip')
with post_path.open('wb') as post_file:
post_file.write(post_data)
self._set_response()
self.wfile.write(f"POST request for {self.path}".encode())
def run(host='0.0.0.0', port=80):
httpd = HTTPServer((host, port), Server)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
run()