31 lines
1017 B
Python
31 lines
1017 B
Python
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() |