Skip to content

Commit

Permalink
Add functionality to bind to a specific IP address. Fixes #36
Browse files Browse the repository at this point in the history
  • Loading branch information
sivel committed Nov 11, 2013
1 parent 79aeabe commit d26cf87
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions speedtest_cli.py
Expand Up @@ -18,9 +18,9 @@
__version__ = '0.2.2'

try:
from urllib2 import urlopen, Request, HTTPError
from urllib2 import urlopen, Request, HTTPError, URLError
except ImportError:
from urllib.request import urlopen, Request, HTTPError
from urllib.request import urlopen, Request, HTTPError, URLError

import math
import time
Expand All @@ -29,6 +29,11 @@
import threading
import re
import signal
import socket

# Used for bound_interface
socket_socket = socket.socket

from xml.dom import minidom as DOM

try:
Expand Down Expand Up @@ -116,6 +121,15 @@ def write(data):
del builtins


def bound_socket(*args, **kwargs):
"""Bind socket to a specified source IP address"""

global source
sock = socket_socket(*args, **kwargs)
sock.bind((source, 0))
return sock


def distance(origin, destination):
"""Determine distance between 2 sets of [lat,lon] in km"""

Expand Down Expand Up @@ -359,7 +373,7 @@ def version():
def speedtest():
"""Run the full speedtest.net test"""

global shutdown_event
global shutdown_event, source
shutdown_event = threading.Event()

signal.signal(signal.SIGINT, ctrl_c)
Expand Down Expand Up @@ -387,6 +401,7 @@ def speedtest():
'sorted by distance')
parser.add_argument('--server', help='Specify a server ID to test against')
parser.add_argument('--mini', help='URL of the Speedtest Mini server')
parser.add_argument('--source', help='Source IP address to bind to')
parser.add_argument('--version', action='store_true',
help='Show the version number and exit')

Expand All @@ -400,9 +415,17 @@ def speedtest():
if args.version:
version()

if args.source:
source = args.source
socket.socket = bound_socket

if not args.simple:
print_('Retrieving speedtest.net configuration...')
config = getConfig()
try:
config = getConfig()
except URLError:
print_('Cannot retrieve speedtest configuration')
sys.exit(1)

if not args.simple:
print_('Retrieving speedtest.net server list...')
Expand Down

0 comments on commit d26cf87

Please sign in to comment.