Update page 'Day 8 homework - Karen'

2023-04-14 16:54:09 +02:00
parent 4f763bd08e
commit a6210ea262
+33
@@ -33,3 +33,36 @@ if __name__ == '__main__':
![Flask](https://gogs.gtsplab.com:5432/j/python-bootcamp/raw/Branch_Karen/flack.png)
I also try to use fastapi this morning, and the result turns to be like this:
![fastapi](https://gogs.gtsplab.com:5432/j/python-bootcamp/raw/Branch_Karen/fastapi.png)
```
import os
from fastapi import FastAPI
from fastapi.responses import FileResponse
from urllib.parse import urlparse
app = FastAPI()
@app.get('/')
def index():
return 'Hello, welcome to the cat picture server!'
@app.get('/{path:path}')
async def serve_jpeg(path: str):
if path.endswith('.jpg'):
file_path = path[1:]
if os.path.isfile(file_path):
return FileResponse(file_path, media_type='image/jpeg')
else:
return {'detail': 'File not found'}, 404
else:
return {'detail': 'Only .jpg files are allowed'}, 403
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8000)
```