Skip to content
This repository has been archived by the owner on Feb 10, 2019. It is now read-only.

Commit

Permalink
fixes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
septs committed Mar 31, 2016
1 parent 80421fa commit 47fc798
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/apple-cdn-speed.report
22 changes: 13 additions & 9 deletions export-configure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#!/usr/bin/env python3
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function, unicode_literals

import json
import os.path
import sys
from argparse import ArgumentParser
from collections import defaultdict

from io import open

if sys.version_info[0] == 2:
str = unicode

formats = {
'hosts': '{ip:<15} {domain}',
Expand All @@ -10,8 +21,6 @@


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.')
Expand All @@ -25,7 +34,6 @@ def check_python_version():


def find_fast_ip(ips):
from collections import defaultdict
table = defaultdict(list)
for item in sum(ips.values(), []):
table[item['ip']].append(item['delta'])
Expand Down Expand Up @@ -53,17 +61,13 @@ def export(payload, target):


def load_payload():
import json
import os.path
from io import open
target_filename = 'apple-cdn-speed.report'
if os.path.exists(target_filename):
return json.load(open(target_filename, encoding='UTF-8'))
print('please run "fetch-timeout.py" build "%s".' % target_filename)


def main():
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument(
'target',
Expand Down
72 changes: 39 additions & 33 deletions fetch-timeout.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
#!/usr/bin/env python3
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function, unicode_literals

import json
import multiprocessing
import os.path
import random
import socket
import sys
from argparse import ArgumentParser
from contextlib import closing
from datetime import datetime

from io import open

if sys.version_info[0] == 2:
from urlparse import urlparse
str = unicode
else:
from urllib.parse import urlparse

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.')
Expand All @@ -24,13 +40,10 @@ def 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 = socket.socket()
conn.settimeout(timeout / 1000.0)
conn.connect((host, port))

Expand All @@ -40,31 +53,26 @@ def request(target):

rt = (delta.seconds * 1000) + (delta.microseconds / 1000.0)
return host, rt
except error as err:
except socket.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

def handle_ip(target):
address = urlparse('http://%s' % str(target))
return address.hostname, address.port or 80

with closing(multiprocessing.Pool(concurrent)) as pool:
for service_item in payload:
print(service_item['title'])
print(str(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 = ips * testing_times
random.shuffle(ips)
ips = pool.map(request, map(handle_ip, ips))
ips = sorted(
({'ip': ip, 'delta': delta} for ip, delta in ips if delta),
key=lambda item: item['delta']
Expand All @@ -77,26 +85,24 @@ def fetch(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'))
with open(target_filename, 'w', encoding='utf-8') as fp:
report_data = json.dumps(
payload,
sort_keys=True,
indent=4,
ensure_ascii=False
)
fp.write(str(report_data))


def main():
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('payload', help='payload')
args = parser.parse_args()
Expand Down

0 comments on commit 47fc798

Please sign in to comment.