v.3
This commit is contained in:
parent
412ae04909
commit
c10129a5a6
45
main.py
45
main.py
@ -1,37 +1,18 @@
|
|||||||
import socket
|
from fastapi import FastAPI
|
||||||
|
|
||||||
def generate_html_response(count):
|
app = FastAPI()
|
||||||
html_content = """
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Страница счетчика</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Счетчик: {}</h1>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
""".format(count)
|
|
||||||
return html_content
|
|
||||||
|
|
||||||
serv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
serv_sock.bind(('0.0.0.0', 12345))
|
|
||||||
serv_sock.listen(5)
|
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
while True:
|
@app.get("/count")
|
||||||
client_sock, client_addr = serv_sock.accept()
|
async def get_count():
|
||||||
data = client_sock.recv(1024)
|
global count
|
||||||
request = data.decode()
|
count += 1
|
||||||
print(request)
|
return {"count": count}
|
||||||
|
|
||||||
if "GET /count" in request:
|
@app.get("/")
|
||||||
count += 1
|
async def root():
|
||||||
html_response = generate_html_response(count)
|
return {"message": "Hello World"}
|
||||||
http_response = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n" + html_response
|
if __name__ == "__main__":
|
||||||
client_sock.sendall(http_response.encode())
|
import uvicorn
|
||||||
else:
|
uvicorn.run(app, host="0.0.0.0", port=12345)
|
||||||
http_response = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n "
|
|
||||||
client_sock.sendall(http_response.encode())
|
|
||||||
|
|
||||||
client_sock.close()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user