【问题标题】:problem with xmlrpc serverxmlrpc 服务器的问题
【发布时间】:2011-08-05 13:22:12
【问题描述】:

我使用 xmlrpc 服务器运行简单示例,然后按键盘上的 Ctrl-C :)。

从 SimpleXMLRPCServer 导入 SimpleXMLRPCServer 从时间导入睡眠 导入线程,时间 类测试(线程。线程): def __init__(self): threading.Thread.__init__(self) 自我测试1 = 0 定义测试(自我): 返回 self.test1 定义运行(自我): 而(1): 时间.sleep(1) self.test1 = self.test1 + 1 ts = 测试() ts.start() 服务器 = SimpleXMLRPCServer(("localhost",8888)) server.register_instance(ts) server.serve_forever()

按键盘后出错:

文件“/usr/lib/python2.7/SocketServer.py”,第 225 行,在 serve_forever r, w, e = select.select([self], [], [], poll_interval) 键盘中断

客户

从 xmlrpclib 导入 ServerProxy r=ServerProxy("http://localhost:8888") 打印 r.test() 等待连接没有错误或警告。在这种情况下如何断开连接? 也许这个例子不正确?

【问题讨论】:

    标签: python xmlrpclib


    【解决方案1】:

    使用超时:

    Set timeout for xmlrpclib.ServerProxy

    编辑

    此处链接的答案与 Python 2.7 不兼容。这是修改后的代码(在 W7/ActivePython 2.7 上测试):

    import xmlrpclib
    import httplib
    
    class TimeoutHTTPConnection(httplib.HTTPConnection):
    
        def __init__(self,host,timeout=10):
            httplib.HTTPConnection.__init__(self,host,timeout=timeout)
            self.set_debuglevel(99)
            #self.sock.settimeout(timeout)
    
    """
    class TimeoutHTTP(httplib.HTTP):
        _connection_class = TimeoutHTTPConnection
        def set_timeout(self, timeout):
            self._conn.timeout = timeout
    """
    
    class TimeoutTransport(xmlrpclib.Transport):
        def __init__(self, timeout=10, *l, **kw):
            xmlrpclib.Transport.__init__(self,*l,**kw)
            self.timeout=timeout
    
        def make_connection(self, host):
            conn = TimeoutHTTPConnection(host,self.timeout)
            return conn
    
    class TimeoutServerProxy(xmlrpclib.ServerProxy):
        def __init__(self,uri,timeout=10,*l,**kw):
            kw['transport']=TimeoutTransport(timeout=timeout, use_datetime=kw.get('use_datetime',0))
            xmlrpclib.ServerProxy.__init__(self,uri,*l,**kw)
    
    if __name__ == "__main__":
        s=TimeoutServerProxy('http://127.0.0.1:8888',timeout=2)
        print s.test()
    

    【讨论】:

    • this don't work wish python 2.7
       response = h.getresponse(buffering=True) AttributeError: TimeoutHTTP instance has no attribute 'getresponse' 
    【解决方案2】:

    使您的Test 实例成为守护进程,在主线程退出时退出:

    ts = Test()
    ts.setDaemon(True)
    ts.start()
    

    问题是为什么需要将线程注册为 XML-RPC 处理程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 2014-05-19
      • 2013-02-11
      • 1970-01-01
      • 2014-08-19
      相关资源
      最近更新 更多