【问题标题】:URLLib2 urlopen is not working if we run Python script as win32 service(Windows Service)如果我们将 Python 脚本作为 win32 服务(Windows 服务)运行,则 URLLib2 urlopen 不起作用
【发布时间】: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


【解决方案1】:

您的防火墙似乎正在采取措施。 尝试关闭防火墙,如果有帮助,您应该通过允许端口或服务(按其名称)为该服务关闭它。

详见微软帮助页面: https://technet.microsoft.com/en-us/library/cc757622(v=ws.10).aspx

【讨论】:

  • 防火墙已为所有服务关闭,即使它不工作。
猜你喜欢
  • 2010-09-07
  • 2017-08-01
  • 2010-11-28
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多