正在检测外部IP是否随miniupnpc发生变化。在我的测试过程中,我注意到当我关闭Wi-Fi (断开我的pc与互联网的连接)时,我得到了一个错误,我无法捕捉到异常。
它似乎已打印到控制台。
u = miniupnpc.UPnP()
u.discoverdelay = config.svc_upnp_disc_delay
try:
discover = u.discover()
print(discover, 'device(s) detected')
if discover:
u.selectigd()
while True:
stat_info, stat_port, stat_error = u.statusinfo()
if stat_info == 'Connected':
if u.lanaddr != os.environ.get(config.envr_name_int_addr):
os.environ[config.envr_name_int_addr] = u.lanaddr
ext_ip = u.externalipaddress()
if ext_ip != os.environ.get(config.envr_name_ext_addr):
os.environ[config.envr_name_ext_addr] = ext_ip
print('+', end=' ', flush=True)
else:
print('-', end=' ', flush=True)
if stat_info != os.environ.get(config.envr_svc_status):
os.environ[config.envr_svc_status] = stat_info
time.sleep(5)
except Exception as e:
print('Exception :', e)这是错误:
sendto: Network is unreachable
sendto: Network is unreachable
sendto: Network is unreachable
sendto: Network is unreachable有没有办法捕获它,这样我就可以将它记录在日志文件中,而不是控制台中?
在第一个place...because中我没有打印它的原因是什么?
发布于 2021-01-13 06:21:32
有4个错误被记录这一事实表明您的try...except块可能无法正常工作。
尝试在except子句中使用BaseException而不是Exception。
https://stackoverflow.com/questions/65692369
复制相似问题