This commit is contained in:
yumoqing 2023-06-25 10:35:31 +08:00
parent 9b4fb8c138
commit 1066e2250f

25
appPublic/uni_outip.py Normal file → Executable file
View File

@ -6,23 +6,32 @@ from appPublic.ipgetter import IPgetter
from multiprocessing import Process, Pipe from multiprocessing import Process, Pipe
def pmp_get_external_ip(): def pmp_get_external_ip():
return pmp.get_public_address() try:
return pmp.get_public_address()
except:
return None
def upnp_get_external_ip(): def upnp_get_external_ip():
igd = upnpclient.discover()[0] try:
s_names = [ n for n in igd.service_map.keys() if 'WAN' in n and 'Conn' in n] igd = upnpclient.discover()[0]
upnp = igd.service_map[s_names[0]] s_names = [ n for n in igd.service_map.keys() if 'WAN' in n and 'Conn' in n]
x = upnp.GetExternalIPAddress() upnp = igd.service_map[s_names[0]]
return x.get('NewExternalIPAddress', None) x = upnp.GetExternalIPAddress()
return x.get('NewExternalIPAddress', None)
except:
return None
def ipgetter_get_external_ip(): def ipgetter_get_external_ip():
getter = IPgetter() getter = IPgetter()
ip = None ip = None
while ip is None: while ip is None:
ip = getter.get_external_ip() try:
ip = getter.get_external_ip()
except:
ip = None
if ip: if ip:
return ip return ip
time.sleep(0.5) time.sleep(0.1)
def get_external_ip(): def get_external_ip():
ip = pmp_get_external_ip() ip = pmp_get_external_ip()