From 59d742baf520514883e6e5ef0b779c6b84fbbe5e Mon Sep 17 00:00:00 2001 From: Karenzz Date: Fri, 14 Apr 2023 00:45:54 +0200 Subject: [PATCH] Update page 'Day 8 homework - Karen' --- Day 8 homework - Karen.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Day 8 homework - Karen.md diff --git a/Day 8 homework - Karen.md b/Day 8 homework - Karen.md new file mode 100644 index 0000000..6f28f97 --- /dev/null +++ b/Day 8 homework - Karen.md @@ -0,0 +1,35 @@ +The original graph is shown below. + +![Original](https://gogs.gtsplab.com:5432/j/python-bootcamp/raw/Branch_Karen/original.png) + +We can use Flask in python to serve cat pictures as fast as possible, where Flask is used as the web framework to handle incoming HTTP requests. + +``` +import os +from flask import Flask, Response +from urllib.parse import urlparse + +app = Flask(__name__) + +@app.route('/') +def index(): + return 'Hello, welcome to the cat picture server!' + +@app.route('/') +def serve_jpeg(path): + if path.endswith('.jpg'): + file_path = path[1:] + if os.path.isfile(file_path): + with open(file_path, 'rb') as file: + return Response(file.read(), content_type='image/jpeg') + else: + return 'File not found', 404 + else: + return 'Only .jpg files are allowed', 403 + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=8000) +``` + +![Flask](https://gogs.gtsplab.com:5432/j/python-bootcamp/raw/Branch_Karen/flack.png) +