我希望我的 python 脚本检查事件的互联网连接,如果有则继续执行。如果没有连接,则继续检查。基本上阻止“main()”中的执行,直到脚本可以重新连接。
python
import urllib2
def main():
#the script stuff
def internet_on():
try:
response=urllib2.urlopen('http://74.125.113.99',timeout=1)
main()
except urllib2.URLError:
internet_on()
最佳答案
绝对不要递归地做。改用循环:
def wait_for_internet_connection():
while True:
try:
response = urllib2.urlopen('http://74.125.113.99',timeout=1)
return
except urllib2.URLError:
pass
def main():
...
wait_for_internet_connection()
main()
关于python 等到连接激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10609358/