Skip to content

Instantly share code, notes, and snippets.

@Gabrielgtt
Last active November 22, 2019 03:56
Show Gist options
  • Save Gabrielgtt/3101659fb3c03c01ce9077bbf6965108 to your computer and use it in GitHub Desktop.
Save Gabrielgtt/3101659fb3c03c01ce9077bbf6965108 to your computer and use it in GitHub Desktop.
Laboratório de redes 2019.2
#coding:utf-8
import BaseHTTPServer
import os
import shutil
import sys
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
try:
with open(self.path[1:], 'rb') as f:
print("File was found")
self.send_response(200)
self.send_header("Content-Type", 'application/octet-stream')
self.send_header("Content-Disposition", 'attachment; filename="{}"'.format(os.path.basename(self.path[1:])))
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs.st_size))
self.end_headers()
shutil.copyfileobj(f, self.wfile)
except:
print("Filé not found")
self.send_response(404)
self.send_header("Content-Type", 'text/html')
self.end_headers()
self.wfile.write("<html><head><title>404 :(</title></head>")
self.wfile.write("<body><p>File not found!</p>")
def start(HandlerClass=SimpleHTTPRequestHandler, ServerClass=BaseHTTPServer.HTTPServer, protocol="HTTP/1.0"):
port = 8000
server_address = ('', port)
HandlerClass.protocol_version = protocol
httpd = BaseHTTPServer.HTTPServer(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print("Listening...")
httpd.serve_forever()
if __name__ == '__main__':
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment