xmlrpc 服务器问题
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用超时:
为 xmlrpclib.ServerProxy 设置超时
编辑
此处链接的答案与 Python 2.7 不兼容。以下是修改后的有效代码(在 W7/ActivePython 2.7 上测试):
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):
让您的
Test
实例成为守护进程,以便在主线程退出时退出:问题是为什么您需要将线程注册为 XML-RPC 处理程序。
Make your
Test
instance daemonic, to quit when the main thread quits:The question is why you need to register a thread as an XML-RPC handler.