Created
February 23, 2023 08:05
-
-
Save airvzxf/0e9e73079c5f7d559a2ba24be9a1704e 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
#!/usr/bin/env python3 | |
# -*- coding: UTF-8 -*- | |
""" | |
Convert String into Array of Strings. | |
""" | |
from http import server | |
from socketserver import ThreadingMixIn | |
from hello_world import hello, world | |
# Open the URL: http://localhost:8001/ | |
class RequestHandler(server.BaseHTTPRequestHandler): | |
def do_GET(self): | |
print(f"self.path: {self.path}") | |
if self.path == '/': | |
self.send_response(301) | |
self.send_header('Location', '/index.html') | |
self.end_headers() | |
elif self.path == '/index.html': | |
self.generate_index() | |
elif self.path == '/image.jpg': | |
self.generate_image() | |
else: | |
self.send_error(404) | |
self.end_headers() | |
def generate_index(self): | |
pagina_html = open("index.html").read() | |
content = pagina_html.encode('utf-8') | |
self.send_response(200) | |
self.send_header('Content-Type', 'text/html') | |
self.send_header('Content-Length', len(content).__str__()) | |
self.end_headers() | |
self.wfile.write(content) | |
def generate_image(self): | |
self.send_response(200) | |
self.send_header('Age', '0') | |
self.send_header('Cache-Control', 'no-cache, private') | |
self.send_header('Pragma', 'no-cache') | |
self.send_header('Content-Type', 'image/jpeg') | |
self.end_headers() | |
image = open("./image.jpg", "rb").read() | |
self.wfile.write(image) | |
class ThreadedHTTPServer(ThreadingMixIn, server.HTTPServer): | |
allow_reuse_address = True | |
daemon_threads = True | |
if __name__ == '__main__': | |
# Ctrl+Alt+S: Show the packages from Python interpreter | |
hello() | |
world() | |
address = ('', 8001) | |
server = ThreadedHTTPServer(address, RequestHandler) | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment