Merge a369250a14
into 795bc51da4
This commit is contained in:
commit
9df2f45521
|
@ -33,6 +33,16 @@ Display values in bytes instead of bits. Does not affect the image generated by
|
|||
Generate and provide a URL to the speedtest.net share results image
|
||||
.RE
|
||||
|
||||
\fB\-\-uid USERNAME\fR
|
||||
.RS
|
||||
Supply speedtest.net username so that shared results will be listed in the speedtest.net "My Results" page. Must also supply --pw. Implies --share.
|
||||
.RE
|
||||
|
||||
\fB\-\-pw PASSWORD\fR
|
||||
.RS
|
||||
Supply speedtest.net password
|
||||
.RE
|
||||
|
||||
\fB\-\-simple\fR
|
||||
.RS
|
||||
Suppress verbose output, only show basic information
|
||||
|
|
|
@ -44,9 +44,11 @@ except ImportError:
|
|||
|
||||
# Begin import game to handle Python 2 and Python 3
|
||||
try:
|
||||
from urllib2 import urlopen, Request, HTTPError, URLError
|
||||
from urllib2 import urlopen, Request, HTTPError, URLError, \
|
||||
HTTPErrorProcessor, build_opener
|
||||
except ImportError:
|
||||
from urllib.request import urlopen, Request, HTTPError, URLError
|
||||
from urllib.request import urlopen, Request, HTTPError, URLError, \
|
||||
HTTPErrorProcessor, build_opener
|
||||
|
||||
try:
|
||||
from httplib import HTTPConnection, HTTPSConnection
|
||||
|
@ -63,6 +65,11 @@ try:
|
|||
except ImportError:
|
||||
from urllib.parse import urlparse
|
||||
|
||||
try:
|
||||
from urllib import urlencode
|
||||
except ImportError:
|
||||
from urllib.parse import urlencode
|
||||
|
||||
try:
|
||||
from urlparse import parse_qs
|
||||
except ImportError:
|
||||
|
@ -165,6 +172,15 @@ def distance(origin, destination):
|
|||
return d
|
||||
|
||||
|
||||
class NoRedirection(HTTPErrorProcessor):
|
||||
"""httplib class to ignore redirections"""
|
||||
|
||||
def http_response(self, request, response):
|
||||
return response
|
||||
|
||||
https_response = http_response
|
||||
|
||||
|
||||
class FileGetter(threading.Thread):
|
||||
"""Thread class for retrieving a URL"""
|
||||
|
||||
|
@ -446,6 +462,35 @@ def version():
|
|||
raise SystemExit(__version__)
|
||||
|
||||
|
||||
def speedtest_auth(uid, pw):
|
||||
"""Log in to speedtest.net"""
|
||||
|
||||
authlist = []
|
||||
|
||||
noredir = build_opener(NoRedirection)
|
||||
|
||||
postData = urlencode({'email': uid, 'password': pw, 'action': 'login'})
|
||||
|
||||
req = Request('https://www.speedtest.net/user-login.php', postData)
|
||||
f = noredir.open(req)
|
||||
cookies = f.info().getallmatchingheaders('Set-Cookie')
|
||||
code = f.code
|
||||
f.close()
|
||||
|
||||
if int(code) != 302:
|
||||
print_('Could not log in to speedtest.net')
|
||||
sys.exit(1)
|
||||
|
||||
for cookieline in cookies:
|
||||
cookieline = re.sub("(\r|\n)", "", cookieline)
|
||||
cookieline = re.sub("^Set-Cookie:\s*", "", cookieline)
|
||||
cookieline = re.sub(";.*$", "", cookieline)
|
||||
if re.match("(stnetsid|expire)=", cookieline):
|
||||
authlist.append(cookieline)
|
||||
|
||||
return "; ".join(authlist)
|
||||
|
||||
|
||||
def speedtest():
|
||||
"""Run the full speedtest.net test"""
|
||||
|
||||
|
@ -472,6 +517,8 @@ def speedtest():
|
|||
const=('bytes', 1), default=('bits', 8),
|
||||
help='Display values in bytes instead of bits. Does '
|
||||
'not affect the image generated by --share')
|
||||
parser.add_argument('--uid', help='Specify speedtest.net username')
|
||||
parser.add_argument('--pw', help='Specify speedtest.net password')
|
||||
parser.add_argument('--share', action='store_true',
|
||||
help='Generate and provide a URL to the speedtest.net '
|
||||
'share results image')
|
||||
|
@ -503,6 +550,17 @@ def speedtest():
|
|||
source = args.source
|
||||
socket.socket = bound_socket
|
||||
|
||||
if (args.uid and not args.pw) or (args.pw and not args.uid):
|
||||
print_('Must specify both uid and pw or neither')
|
||||
sys.exit(1)
|
||||
|
||||
if args.uid:
|
||||
if not args.share:
|
||||
print_('Warning: specifying credentials implies --share')
|
||||
auth = speedtest_auth(args.uid, args.pw)
|
||||
else:
|
||||
auth = None
|
||||
|
||||
if not args.simple:
|
||||
print_('Retrieving speedtest.net configuration...')
|
||||
try:
|
||||
|
@ -638,7 +696,7 @@ def speedtest():
|
|||
if args.share and args.mini:
|
||||
print_('Cannot generate a speedtest.net share results image while '
|
||||
'testing against a Speedtest Mini server')
|
||||
elif args.share:
|
||||
elif args.share or args.uid:
|
||||
dlspeedk = int(round((dlspeed / 1000) * 8, 0))
|
||||
ping = int(round(best['latency'], 0))
|
||||
ulspeedk = int(round((ulspeed / 1000) * 8, 0))
|
||||
|
@ -662,6 +720,8 @@ def speedtest():
|
|||
req = Request('http://www.speedtest.net/api/api.php',
|
||||
data='&'.join(apiData).encode())
|
||||
req.add_header('Referer', 'http://c.speedtest.net/flash/speedtest.swf')
|
||||
if auth:
|
||||
req.add_header('Cookie', auth)
|
||||
f = urlopen(req)
|
||||
response = f.read()
|
||||
code = f.code
|
||||
|
@ -677,7 +737,9 @@ def speedtest():
|
|||
print_('Could not submit results to speedtest.net')
|
||||
sys.exit(1)
|
||||
|
||||
print_('Share results: http://www.speedtest.net/result/%s.png' %
|
||||
print_('Share results: http://www.speedtest.net/my-result/%s' %
|
||||
resultid[0])
|
||||
print_('Share results image: http://www.speedtest.net/result/%s.png' %
|
||||
resultid[0])
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue