Created
April 26, 2016 23:56
-
-
Save Surye/cb6b6a910f308efd6fa732fd0416fba0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.server | |
import socketserver | |
import os.path | |
def make_http_handler(root_path): | |
class HTTPHandler(http.server.BaseHTTPRequestHandler): | |
def __init__(self, *args, **kwargs): | |
super(HTTPHandler, self).__init__(*args, **kwargs) | |
def do_GET(self): | |
file_path = os.path.abspath(root_path + self.path) | |
if os.path.commonpath([root_path, file_path]) != os.path.abspath(root_path): # Detect path traversal attempt | |
self.send_error(501, "Nice try") | |
else: | |
if not os.path.exists(file_path) or not os.path.isfile(file_path): | |
self.send_error(404, 'File Not Found') | |
else: | |
self.send_response(200) | |
self.wfile.write(open(file_path, mode='rb').read()) | |
return HTTPHandler | |
handler = make_http_handler('C:/tmp/test/') | |
print('Server listening on port 8000...') | |
httpd = socketserver.TCPServer(('', 8000), handler) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment