【发布时间】:2016-02-16 07:26:27
【问题描述】:
我有 URL:“http://google.com”,如果我从命令提示符运行 Python 脚本,那么我们会得到预期的结果。但是如果我们运行与 win32 服务相同的代码,则会出现以下错误
urlopen 错误 [Errno 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)
【问题讨论】:
-
您没有系统级别的代理设置不可用(仅为您的用户设置,而不是为 LocalSystem 设置)?
标签: python-2.7 urllib2 pywin32