xmlrpc 服务器问题

发布于 2024-11-01 11:03:38 字数 967 浏览 4 评论 0原文

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

from SimpleXMLRPCServer import SimpleXMLRPCServer
from time import sleep
import threading,time

class 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 + 1

ts = 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 ServerProxy
r=ServerProxy("http://localhost:8888")
print r.test()
 
waiting connect without error or warning. How to break connection in this case ? Maybe this example is not correct ?

I run simple example with xmlrpc server and press Ctrl-C on keyboard :).

from SimpleXMLRPCServer import SimpleXMLRPCServer
from time import sleep
import threading,time

class 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 + 1

ts = Test()
ts.start()
server =  SimpleXMLRPCServer(("localhost",8888))
server.register_instance(ts)
server.serve_forever()

error after pressing keyboard:

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

Client

 
from xmlrpclib import ServerProxy
r=ServerProxy("http://localhost:8888")
print r.test()
 

waiting connect without error or warning. How to break connection in this case ?
Maybe this example is not correct ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黄昏下泛黄的笔记 2024-11-08 11:03:38

使用超时:

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

Use a timeout:

Set timeout for xmlrpclib.ServerProxy

EDIT

The answer linked to here is not compatible with Python 2.7. Here is modified code that works (tested on 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()
蓝礼 2024-11-08 11:03:38

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

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

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

Make your Test instance daemonic, to quit when the main thread quits:

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

The question is why you need to register a thread as an XML-RPC handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文