-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
106 lines (83 loc) · 2.94 KB
/
parser.py
File metadata and controls
106 lines (83 loc) · 2.94 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
import json
import sys
def parse_chlsj(filename, data):
host = data['host']
path = data['path']
client_address = data['clientAddress']
method = data['method']
request = data['request']
response = data['response']
out_file = open(filename, 'w')
line_sum = '# ' + method + ' ' + host + path
out_file.write(line_sum)
out_file.write('\n\n')
# Request
out_file.write('+ Request')
out_file.write('\n\n')
# request header
out_file.write('\t' + '> Headers' + '\n')
for header in request['header']['headers']:
out_file.write('\t\t')
out_file.write(header['name'])
out_file.write(': ')
out_file.write(header['value'])
out_file.write('\n')
out_file.write('\n\n\n')
# request body
out_file.write('\t' + '> Body' + '\n')
body_content_json = request['body']['text']
parsed_body_json = json.loads(body_content_json)
parsed_body_str = json.dumps(parsed_body_json, indent=4, sort_keys=True)
for line in parsed_body_str.split('\n'):
out_file.write('\t\t')
out_file.write(line)
out_file.write('\n')
out_file.write('\n\n\n')
out_file.write('===========================================================================')
out_file.write('\n\n\n')
# Response
response_code = response['status']
out_file.write('+ Response (' + str(response_code) + ')')
out_file.write('\n\n')
# response header
out_file.write('\t' + '> Headers' + '\n')
for header in response['header']['headers']:
out_file.write('\t\t')
out_file.write(header['name'])
out_file.write(': ')
out_file.write(header['value'])
out_file.write('\n')
out_file.write('\n\n\n')
# response body
out_file.write('\t' + '> Body' + '\n')
body_content_json = response['body']['text']
parsed_body_json = json.loads(body_content_json)
parsed_body_str = json.dumps(parsed_body_json, indent=4, sort_keys=True)
for line in parsed_body_str.split('\n'):
out_file.write('\t\t')
out_file.write(line)
out_file.write('\n')
out_file.write('\n\n\n\n')
out_file.close()
print('file output: '+filename)
def main(args):
in_file_name = args[0]
out_file_name = in_file_name.split('.')[0] + '_parse.txt'
if len(args) > 2:
out_file_name = args[1] + '.txt'
if in_file_name.endswith('.chlsj'):
with open(in_file_name) as in_file:
datas = json.load(in_file)
if len(datas) > 1:
i = 1
for data in datas:
out_file_name_with_no = out_file_name.replace('.txt', '_' + str(i) + '.txt')
parse_chlsj(out_file_name_with_no, data)
i = i + 1
else:
parse_chlsj(out_file_name, datas[0])
print('Process complete!')
else:
print('Unrecognized file type.')
if __name__ == '__main__':
main(sys.argv[1:])