Repository navigation
Expand file tree
/
Copy pathfetch-timeout.py
More file actions
executable file
·108 lines (83 loc) · 2.82 KB
/
Copy pathfetch-timeout.py
File metadata and controls
executable file
·108 lines (83 loc) · 2.82 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
timeout = 400 # unit ms
concurrent = 10
testing_times = 10
def check_requirements():
import sys
def check_python_version():
if sys.hexversion >= 0x2000000 and sys.hexversion <= 0x2070000:
print('your "python" lower than 2.7.0 upgrade.')
return False
if sys.hexversion >= 0x3000000 and sys.hexversion <= 0x3040000:
print('your "python" lower than 3.4.0 upgrade.')
return False
return True
return check_python_version()
def request(target):
host, port = target
from socket import socket
from socket import error
from datetime import datetime
try:
begin_time = datetime.now()
conn = socket()
conn.settimeout(timeout / 1000.0)
conn.connect((host, port))
end_time = datetime.now()
delta = end_time - begin_time
rt = (delta.seconds * 1000) + (delta.microseconds / 1000.0)
return host, rt
except error as err:
return host, False
def handle_ip(target):
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
address = urlparse('http://%s' % target)
return address.hostname, address.port or 80
def fetch(payload):
if not payload:
return
import multiprocessing
from contextlib import closing
with closing(multiprocessing.Pool(concurrent)) as pool:
for service_item in payload:
print(service_item['title'])
print(', '.join(service_item['domains']))
for name, ips in service_item['ips'].items():
ips = pool.map(request, map(handle_ip, ips * testing_times))
ips = sorted(
({'ip': ip, 'delta': delta} for ip, delta in ips if delta),
key=lambda item: item['delta']
)
service_item['ips'][name] = ips
print('\t%s' % name)
for item in ips:
print('\t\t%(ip)-15s\t%(delta)sms' % item)
return payload
def load_payload(path):
import json
import os.path
from io import open
if os.path.exists(path):
with open(path, encoding='UTF-8') as fp:
return json.loads(fp.read())
def save_result(payload):
import json
from io import open
target_filename = 'apple-cdn-speed.report'
try:
json.dump(payload, open(target_filename, 'wb'))
except:
json.dump(payload, open(target_filename, 'w'))
def main():
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('payload', help='payload')
args = parser.parse_args()
save_result(fetch(load_payload(args.payload)))
if __name__ == '__main__' and check_requirements():
main()