gpt4 book ai didi

python - xmlrpc 服务器的问题

转载 作者:行者123 更新时间:2023-11-28 17:53:24 25 4
gpt4 key购买 nike

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

from SimpleXMLRPCServer import SimpleXMLRPCServerfrom time import sleepimport threading,timeclass Test(threading.Thread):    def __init__(self):        threading.Thread.__init__(self)        self.test1 = 0    def test(self):        return self.test1    def run(self):        while(1):            time.sleep(1)            self.test1 = self.test1 + 1ts = Test()ts.start()server =  SimpleXMLRPCServer(("localhost",8888))server.register_instance(ts)server.serve_forever()

按下键盘后出错:

  File "/usr/lib/python2.7/SocketServer.py", line 225, in serve_forever    r, w, e = select.select([self], [], [], poll_interval)KeyboardInterrupt

客户端

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

最佳答案

使用超时:

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()

关于python - xmlrpc 服务器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5663946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com