online examples suck

master
JoYo 2020-08-12 18:15:00 -04:00
parent 1b1e2b2f3d
commit 5d096cd67d
1 changed files with 46 additions and 0 deletions

46
server.py Normal file
View File

@ -0,0 +1,46 @@
#!/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()