-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathparse_text_http_headers.py
55 lines (43 loc) · 1.75 KB
/
parse_text_http_headers.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
import re
HTTP_HEADER_PATTERN = re.compile(r"([\w-]+): (.*)", flags=re.IGNORECASE)
def parse(text: str) -> dict[str, str]:
return dict(HTTP_HEADER_PATTERN.findall(text))
if __name__ == "__main__":
text_http_headers = """
POST /index.php?do=search HTTP/1.1
Host: online.anidub.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate, br
Content-Type: application/x-www-form-urlencoded
Content-Length: 112
Origin: https://online.anidub.com
Connection: keep-alive
Referer: https://online.anidub.com/index.php?do=search
Upgrade-Insecure-Requests: 1
TE: Trailers
Pragma: no-cache
Cache-Control: no-cache
"""
headers = parse(text_http_headers)
print(headers)
assert headers == {
"Host": "online.anidub.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": "112",
"Origin": "https://online.anidub.com",
"Connection": "keep-alive",
"Referer": "https://online.anidub.com/index.php?do=search",
"Upgrade-Insecure-Requests": "1",
"TE": "Trailers",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
}