2012-05-15 2 views
3

파이썬 스크립트가 활성 인터넷 연결을 확인하고 실행중인 경우 계속 실행합니다. 연결이 없으면 계속 확인하십시오. 기본적으로 스크립트가 다시 연결될 때까지 "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() 
+4

스택 오버플로가 개인 연구 조교되지 않습니다 : http://meta.stackexchange.com/a/128553 주석에 대한 –

+2

^^ 감사합니다 ... – Blainer

답변

4

파이썬은 확실히 재귀하지 않습니다. 대신 루프를 사용

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() 
관련 문제