python等待,直到连接处于活动状态

python等待,直到连接处于活动状态,python,urllib,Python,Urllib,我希望我的python脚本检查是否有活动的internet连接,如果有,则继续执行。如果没有连接,则继续检查。基本上在“main()”中阻止执行,直到脚本可以重新连接 蟒蛇 import urllib2 def main(): #the script stuff def internet_on(): try: response=urllib2.urlopen('http://74.125.113.99',timeout=1) main()

我希望我的python脚本检查是否有活动的internet连接,如果有,则继续执行。如果没有连接,则继续检查。基本上在“main()”中阻止执行,直到脚本可以重新连接

蟒蛇

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()

堆栈溢出不是您的个人研究助手:这是否回答了您的问题?