Created
March 8, 2014 17:45
-
-
Save curzona/9435751 to your computer and use it in GitHub Desktop.
Recipe for testing a xmlrpc interface with server and client.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import socket | |
from threading import Thread | |
if sys.version_info >= (3,0): | |
from xmlrpc.server import SimpleXMLRPCServer | |
from xmlrpc.client import ServerProxy | |
else: | |
from SimpleXMLRPCServer import SimpleXMLRPCServer | |
from xmlrpclib import ServerProxy | |
socket.setdefaulttimeout(1) | |
class RemoteFunctions: | |
def call(self): | |
return "RemoteFunctions.call()" | |
class Server(): | |
def __init__(self, host, port): | |
self.host = host | |
self.port = port | |
def __del__(self): | |
self.stop() | |
def start(self): | |
self.running = True | |
def inner(): | |
server = SimpleXMLRPCServer((self.host, self.port), | |
allow_none=True) | |
server.register_introspection_functions() | |
server.register_instance(RemoteFunctions()) | |
while self.running: | |
server.handle_request() | |
t = Thread(target=inner) | |
t.start() | |
def stop(self): | |
self.running = False | |
class Client(): | |
def __init__(self, host, port): | |
self.host = host | |
self.port = port | |
def __del__(self): | |
self.close() | |
def connect(self): | |
self.server = ServerProxy('http://%s:%s' % (self.host, self.port)) | |
print(self.server.system.listMethods()) | |
def close(self): | |
pass | |
def call(self): | |
return self.server.call() | |
def test_xmlrpclib(): | |
host, port = 'localhost', 8000 | |
s = Server(host, port) | |
s.start() | |
c = Client(host, port) | |
c.connect() | |
assert c.call() == "RemoteFunctions.call()" | |
s.stop() | |
if __name__ == '__main__': | |
import pytest | |
pytest.main('--verbose') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment