-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp10.py
74 lines (59 loc) · 1.71 KB
/
http10.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
# HTTP 1.0 headers
import platform
from datetime import datetime
def get_http_request(
method: str, file_name: str, data: str, host: str, user_agent: str
) -> str:
"""_summary: construct the header of the http request
Args:
method (str): GET/POST.
file_name (str): requested file.
host (str): the server.
user_agent (str): the user agent of the client.
Returns:
message (str): the request of the http 1.0
"""
message = f"{method} /{file_name} HTTP/1.0\nHost: {host}\nUser-Agent: {user_agent}"
if method == "POST":
message += f"Content-Type: text/html\nContent-Length: {len(data)}\n\n{data}"
return message
def get_http_response(method, status_code, status, data, server):
"""_summary: construct the header of the http response
Args:
method (str): GET/POST
status_code (str): 200/404
status (str): OK/NOT FOUND
data (str): used in case of GET to send the requested file not used in case of POST.
server (str): the server.
Returns:
header (str): the response header of the http 1.0
"""
system = platform.uname()
now = datetime.now()
header = (
"HTTP/1.0 "
+ str(status_code)
+ " "
+ status
+ "\n"
+ "Date: "
+ now.strftime("%a, %d %b %Y %H:%M:%S %Z")
+ "\n"
+ "Server: "
+ server
+ " ("
+ system[0]
+ " "
+ system[2]
+ " "
+ system[4]
+ ")\n"
+ "Content-Length: "
+ str(len(data))
+ "\n"
+ "Content-Type: "
+ "text/plain"
)
if method == "GET":
header = header + "\n\n" + data
return header