Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. def get_webhost(cherryhost, cherryport, https_port):
  2.     """ Determine the webhost address and port,
  3.        return (host, port, browserhost)
  4.    """
  5.     if cherryhost is None:
  6.         cherryhost = sabnzbd.cfg.CHERRYHOST.get()
  7.     else:
  8.         sabnzbd.cfg.CHERRYHOST.set(cherryhost)
  9.  
  10.     # Get IP address, but discard APIPA/IPV6
  11.     # If only APIPA's or IPV6 are found, fall back to localhost
  12.     ipv4 = ipv6 = False
  13.     localhost = hostip = 'localhost'
  14.     try:
  15.         info = socket.getaddrinfo(socket.gethostname(), None)
  16.     except:
  17.         # Hostname does not resolve, use 0.0.0.0
  18.         cherryhost = '0.0.0.0'
  19.         info = socket.getaddrinfo(localhost, None)
  20.     for item in info:
  21.         ip = item[4][0]
  22.         if ip.startswith('169.254.'):
  23.             pass # Is an APIPA
  24.         elif ':' in ip:
  25.             ipv6 = True
  26.         elif '.' in ip and not ipv4:
  27.             ipv4 = True
  28.             hostip = ip
  29.  
  30.     # A blank host will use the local ip address
  31.     if cherryhost == '':
  32.         if ipv6 and ipv4:
  33.             # To protect Firefox users, use numeric IP
  34.             cherryhost = hostip
  35.             browserhost = hostip
  36.         else:
  37.             cherryhost = socket.gethostname()
  38.             browserhost = cherryhost
  39.  
  40.     # 0.0.0.0 will listen on all ipv4 interfaces (no ipv6 addresses)
  41.     elif cherryhost == '0.0.0.0':
  42.         # Just take the gamble for this
  43.         cherryhost = '0.0.0.0'
  44.         browserhost = localhost
  45.  
  46.     # :: will listen on all ipv6 interfaces (no ipv4 addresses)
  47.     elif cherryhost in ('::','[::]'):
  48.         cherryhost = cherryhost.strip('[').strip(']')
  49.         # Assume '::1' == 'localhost'
  50.         browserhost = localhost
  51.  
  52.     # IPV6 address
  53.     elif '[' in cherryhost or ':' in cherryhost:
  54.         browserhost = cherryhost
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement