Browse Source

Move IP address determination into a module sabnzbd.utils.getipaddress

pull/219/head
SanderJ 10 years ago
parent
commit
9dbf41d8f2
  1. 41
      SABnzbd.py
  2. 41
      sabnzbd/utils/getipaddress.py

41
SABnzbd.py

@ -1271,31 +1271,26 @@ def main():
logging.info('Arguments = %s', sabnzbd.CMDLINE)
if sabnzbd.cfg.log_level() > 1:
try:
s_ipv4 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s_ipv4.connect(('1.2.3.4', 80))
logging.debug('My IPv4 address = %s', s_ipv4.getsockname()[0])
s_ipv4.close()
except:
logging.debug('Could not determine my IPv4 address')
pass
from sabnzbd.utils.getipaddress import localipv4, publicipv4, ipv6
try:
import urllib
f = urllib.urlopen("http://api.ipify.org")
public_ipv4 = f.read()
logging.debug('My public IPv4 address = %s', public_ipv4)
except:
logging.debug('Could not determine my public IPv4 address. Error: %s',sys.exc_info())
pass
mylocalipv4 = localipv4()
if mylocalipv4 :
logging.debug('My local IPv4 address = %s', mylocalipv4)
else:
logging.debug('Could not determine my local IPv4 address')
mypublicipv4 = publicipv4()
if mypublicipv4 :
logging.debug('My public IPv4 address = %s', mypublicipv4)
else:
logging.debug('Could not determine my public IPv4 address')
myipv6 = ipv6()
if myipv6 :
logging.debug('My IPv6 address = %s', myipv6)
else:
logging.debug('Could not determine my IPv6 address')
try:
s_ipv6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s_ipv6.connect(('2001:db8::8080', 80))
logging.debug('My IPv6 address = %s', s_ipv6.getsockname()[0])
s_ipv6.close()
except:
logging.debug('Could not determine my IPv6 address. Error: %s',sys.exc_info())
# measure and log Pystone performance
# to avoid a triple nested try/except construction, we use another method:

41
sabnzbd/utils/getipaddress.py

@ -0,0 +1,41 @@
#!/usr/bin/python -OO
import socket
def localipv4():
try:
s_ipv4 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s_ipv4.connect(('1.2.3.4', 80)) # Option: use 100.64.1.1 (IANA-Reserved IPv4 Prefix for Shared Address Space)
ipv4 = s_ipv4.getsockname()[0]
s_ipv4.close()
except:
ipv4 = None
pass
return ipv4
def publicipv4():
try:
import urllib2
#f = urllib.urlopen("http://api.ipify.org") # urllib has no timeout parameter, so:
f = urllib2.urlopen("http://api.ipify.org", timeout=2) # timeout 2 seconds, in case website is not accessible
public_ipv4 = f.read()
except:
public_ipv4 = None
pass
return public_ipv4
def ipv6():
try:
s_ipv6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s_ipv6.connect(('2001:db8::8080', 80)) # IPv6 prefix for documentation purpose
ipv6 = s_ipv6.getsockname()[0]
s_ipv6.close()
except:
ipv6 = None
return ipv6
if __name__ == '__main__':
print localipv4()
print publicipv4()
print ipv6()
Loading…
Cancel
Save