-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
84 lines (66 loc) · 1.9 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from dataclasses import dataclass
import uvicorn
from fastapi import FastAPI, Request, Response
from fastapi.responses import HTMLResponse
METHOD_GET = "GET"
METHOD_POST = "POST"
METHOD_HEAD = "HEAD"
HTML_CODE = """\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>
<body>
<h2>Simple HTML Code</h2>
<h3>Python C API</h3>
<a href="https://github.com/GiorgioMegrelli/">Github</a>
</body>
</html>
"""
@dataclass
class Info:
method: str
url: str
Get = lambda u: Info(METHOD_GET, u)
Post = lambda u: Info(METHOD_POST, u)
Head = lambda u: Info(METHOD_HEAD, u)
def main(*, host: str, port: int) -> None:
app = FastAPI()
html_response = HTMLResponse(content=HTML_CODE, status_code=200)
@app.get("/")
async def root_get():
return Get("/")
@app.head("/")
async def root_head():
return Head("/")
@app.post("/")
async def root_post():
return Post("/")
@app.get("/html")
async def html_get():
return html_response
@app.head("/html")
async def html_head():
return html_response
@app.post("/text")
async def text_post():
return "Just text response on Post Request"
@app.post("/request-header")
async def request_header(request: Request):
test_headers = {
"Request": str(True).lower(),
"Hello": "World",
"Custom-Header-1": "Custom-Header-1-Value",
"Custom-Header-2": "Custom-Header-2-Value",
}
request_headers = dict(request.headers)
result = {}
for k, v in test_headers.items():
kl = k.lower()
result[kl] = kl in request_headers and v == request_headers[kl]
return result
uvicorn.run(app, host=host, port=port, reload=False, debug=True)
if __name__ == "__main__":
main(host="localhost", port=8080)