2016-02-16 29 views
1

我有URL:「http://google.com」,如果我從命令提示符運行Python腳本,那麼我們會得到預期的結果。但是,如果我們運行相同的代碼爲Win32的服務的話,我們得到以下錯誤URLLib2如果我們將Python腳本作爲win32服務運行,則urlopen不起作用(Windows服務)

的urlopen錯誤[錯誤10060]連接嘗試失敗,因爲連接的方沒有正確一段時間後響應或已建立的連接失敗,因爲連接的主機未能響應

代碼段:

class MyService(win32serviceutil.ServiceFramework): 
    """NT Service.""" 

    _svc_name_ = "InstaMessageSyncService" 
    _svc_display_name_ = "InstaMessageSyncService" 

    def __init__(self, args): 
     win32serviceutil.ServiceFramework.__init__(self, args) 
     # create an event that SvcDoRun can wait on and SvcStop 
     # can set. 
     self.stop_event = win32event.CreateEvent(None, 0, 0, None) 

    def SvcDoRun(self): 
     global runProcess 
     try: 
      import urllib2 
      link = "http://www.google.com" 
      log.info("Going to hit URL: %s",link) 
      f = urllib2.urlopen(link) 
      myfile = f.read() 
     except Exception,e: 
      log.info("Exception: %s",e) 
     win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE) 

    def SvcStop(self): 
     runProcess = False 
     self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
     win32event.SetEvent(self.stop_event) 


if __name__ == '__main__': 
    win32serviceutil.HandleCommandLine(MyService) 
+0

不要你的代理服務器設置在系統級別不可用(僅適用於你的用戶,而不是爲了_LocalSystem_設置)? – CristiFati

回答