3

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 ?

0

2 Answers 2

2

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()
Sign up to request clarification or add additional context in comments.

1 Comment

this don't work wish python 2.7 <pre> response = h.getresponse(buffering=True) AttributeError: TimeoutHTTP instance has no attribute 'getresponse' </pre>
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.