This commit is contained in:
2023-04-19 19:11:20 -04:00
committed by Karenzz
parent 9a1ce4965f
commit 252fbee998
5 changed files with 177 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
import os
from http.server import SimpleHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse
class JPEGRequestHandler(SimpleHTTPRequestHandler):
def do_GET(self):
url = urlparse(self.path)
path = url.path
if path.endswith('.jpg'):
file_path = path[1:]
if os.path.isfile(file_path):
self.send_response(200)
self.send_header('Content-Type', 'image/jpeg')
self.end_headers()
with open(file_path, 'rb') as file:
self.wfile.write(file.read())
else:
self.send_error(404, 'File not found')
else:
self.send_error(403, 'Only .jpg files are allowed')
def run(server_class=HTTPServer, handler_class=JPEGRequestHandler, port=8000):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f'Serving JPEGs on port {port}...')
httpd.serve_forever()
if __name__ == '__main__':
run()