forked from jblakeman/apt-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirrors.py
107 lines (92 loc) · 2.98 KB
/
mirrors.py
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
#!/usr/bin/env python
import sys, socket
import time
from re import search
try:
from urllib.request import urlopen, HTTPError
except ImportError:
from urllib2 import urlopen, HTTPError
try:
from bs4 import BeautifulSoup
except ImportError as err:
print(("%s\n"
"Try 'sudo apt-get install python-bs4' "
"or 'sudo apt-get install python3-bs4'" % err))
sys.exit(1)
class RoundTrip:
def __init__(self, url):
self.url = url
def __tcpPing(self):
"""Return latency to hostname"""
port = 80
socket.setdefaulttimeout(2.5)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
addr = socket.gethostbyname(self.url)
except IOError as err:
print("Could not resolve hostname\n%s" % err)
return False
send_tstamp = time.time()*1000
try:
s.connect((addr, port))
except IOError as err:
print(err)
return False
recv_tstamp = time.time()*1000
rtt = recv_tstamp - send_tstamp
s.close()
return rtt
def avgRTT(self):
"""Return average ping (rtt) of 3 if all true"""
rtt = []
for i in range(3):
x = self.__tcpPing()
if x is not False:
rtt += [x]
else:
rtt = []
break
if rtt:
avg = round(sum(rtt) / len(rtt))
return avg
else:
return False
class Data:
def __init__(self, url, codename, hardware):
self.url = url
self.codename = codename
self.hardware = hardware
def __reFind(self, regex, string):
"""Find and return regex match"""
match = search(regex, string)
try:
match = match.group(1)
except AttributeError:
pass
return match
def getInfo(self):
"""Return valid mirror status and bandwidth"""
regex1 = (r'Version\nArchitecture\nStatus\n[\w|\s]'
'+The\s%s\s\w+\n%s\n(.*)\n' % (self.codename, self.hardware))
regex2 = r'Speed:\n([0-9]{1,3}\s\w+)'
archive = "https://launchpad.net/ubuntu/+mirror/%s-archive" % self.url
try:
launch_html = urlopen(archive)
except HTTPError:
try:
launch_html = urlopen(archive.replace('-archive', ''))
except HTTPError:
print(("%s is one of the top mirrors, but "
"has a unique launchpad url.\n"
"Cannot verify, so removed from list" % self.url))
return False
launch_html = launch_html.read().decode()
text = BeautifulSoup(launch_html).get_text()
status = self.__reFind(regex1, text)
if not status or 'unknown' in status:
return False
speed = self.__reFind(regex2, text)
if not speed:
return False
else:
return [self.url, [status, speed]]