-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_converter.py
More file actions
executable file
·110 lines (93 loc) · 4.23 KB
/
Copy pathcli_converter.py
File metadata and controls
executable file
·110 lines (93 loc) · 4.23 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
from pathlib import Path
#import sys
import json
import argparse
def full_HTTP_request_handling(path: Path = None) -> str:
def converting(content: str) -> dict:
result_dict = dict()
try:
if all(method not in content for method in ('GET ', 'POST ', 'PUT ', 'DELETE ', 'OPTIONS ')) or 'Host:' not in content: # simple http validation
raise ValueError
HTTP_request_lines = repr(content)[1:][:-1].strip() # delete start and finish quotes
#HTTP_request_lines = HTTP_request_lines.split('\\r\\n')[2:] # http_req lines without http method and host
HTTP_request_lines = HTTP_request_lines.splitlines()[2:] # тут разваливается
HTTP_request_lines = [line for line in HTTP_request_lines if not line.strip().startswith('Cookie')] # delete cookie from request
for line in HTTP_request_lines:
if ':' not in line:
continue
left_part, right_part = line.split(':', maxsplit=1)
result_dict[left_part.strip()] = right_part.strip()
return result_dict
except ValueError:
print('[!] Wrong request format!\nexiting...')
exit()
if path:
abspath = Path(path).resolve()
try:
with open(abspath, 'r') as file:
content = file.read()
result = converting(content=content)
except FileNotFoundError as ex:
print('[!] File not found! -', ex)
exit()
else:
content = input('Enter full request by pasting\n -> ')
result = converting(content=content)
return result
def cookie_handling(path: Path = None) -> str:
def converting(content: str) -> dict:
result_dict = dict()
try:
cookies = json.loads(content)
result_dict = cookies[list(cookies.keys())[0]] # getting inner dict with cookies like key:value
return result_dict
except Exception as ex:
print(f'[!] Wrong cookies format! - ({ex})\nexiting...')
exit()
if path:
abspath = Path(path).resolve()
try:
with open(abspath, 'r') as file:
content = file.read()
result = converting(content=content)
except FileNotFoundError as ex:
print('[!] File not found! -', ex)
exit()
else:
content = input('Enter cookies from browser dev panel by pasting\n -> ')
result = converting(content=content)
return result
def main():
arg_parser = argparse.ArgumentParser(
description='HTTP & Cookies from browser dev panel to python dict object converter. This dictionary easy to use in data/cookie/headers arguments'
)
arg_parser.add_argument('-i', '--input_file', help='Input text for handling')
arg_parser.add_argument('-o', '--output_file', help='Output file path')
arg_parser.add_argument('-c', '--cookies', help='Cookies handling', action='store_true')
arg_parser.add_argument('-f', '--full_request', help='full request handling', action='store_true')
args = arg_parser.parse_args()
if args.output_file:
if args.cookies:
result = cookie_handling(path=args.input_file)
with open(args.output_file, 'w') as saved_file:
saved_file.write(f'cookies = {result}')
print(f"Dict object saved to file: {args.output_file}")
elif args.full_request:
result = full_HTTP_request_handling(path=args.input_file)
with open(args.output_file, 'w') as saved_file:
saved_file.write(f'headers = {result}')
print(f"Dict object saved to file: {args.output_file}")
else:
if args.cookies:
result = cookie_handling(path=args.input_file)
print("Dict object for python-requests:")
print(f"cookies = {result}")
elif args.full_request:
result = full_HTTP_request_handling(path=args.input_file)
with open(args.output_file, 'w') as saved_file:
saved_file.write(result)
print("Dict object for python-requests:")
print(f"headers = {result}")
if __name__ == '__main__':
main()