-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client.py
More file actions
70 lines (54 loc) · 2.03 KB
/
http_client.py
File metadata and controls
70 lines (54 loc) · 2.03 KB
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
import json
import socket_function
import click
from socket_function import get_do
from urllib.parse import urlparse
COOKIE_FILE = "cookie_jar.json"
def load_cookie():
try:
with open('cookie_jar.json', 'r') as f:
data = json.load(f)
return data
except:
return {}
def save_cookie(data):
with open('cookie_jar.json', 'w') as f:
json.dump(data, f, indent= 4)
def load_cookie_url(url):
try:
host = urlparse(url).hostname
with open('cookie_jar.json', 'r') as f:
data = json.load(f)
return data[f'{host}']
except:
return {}
def save_cookie_url(url, data_url):
host = urlparse(url).hostname
data = load_cookie()
data[f'{host}'] = data_url
save_cookie(data)
with open('cookie_jar.json', 'w') as f:
print('Записали куки')
print(f"{data[f'{host}']}")
json.dump(data, f, indent= 4)
@click.command()
@click.option('--url', required=True, help='URL запроса')
@click.option('--method', default='GET', help='Метод запроса (GET/POST/...')
@click.option('--body', help='Тело запроса')
@click.option('--headers', multiple=True, help='Заголовки ')
@click.option('--timeout', default=15, help='Таймаут на ответ сервера')
@click.option('--output', help='Файл для сохранения данных')
def cli(url, method, body, headers, timeout, output):
cookie = load_cookie_url(url)
try:
headers_dict = {}
for head in headers:
head_split = head.split(':', 1)
headers_dict[head_split[0].strip()] = head_split[1].strip()
updates_cookie = socket_function.get_do(url=url, headers=headers_dict, method=method, body=body, timeout=timeout,
file_name=output, cookie=cookie)
save_cookie_url(url, updates_cookie)
except Exception as exc:
print(f'Произошла ошибка {exc}')
if __name__ == '__main__':
cli()