def server(evt, numrequests):
    serv = DocXMLRPCServer(("localhost", 0), logRequests=False)

    try:
        global PORT
        PORT = serv.socket.getsockname()[1]

        # Add some documentation
        serv.set_server_title("DocXMLRPCServer Test Documentation")
        serv.set_server_name("DocXMLRPCServer Test Docs")
        serv.set_server_documentation(
            "This is an XML-RPC server's documentation, but the server "
            "can be used by POSTing to /RPC2. Try self.add, too."
        )

        # Create and register classes and functions
        class TestClass(object):
            def test_method(self, arg):
                """Test method's docs. This method truly does very little."""
                self.arg = arg

        serv.register_introspection_functions()
        serv.register_instance(TestClass())

        def add(x, y):
            """Add two instances together. This follows PEP008, but has nothing
            to do with RFC1952. Case should matter: pEp008 and rFC1952.  Things
            that start with http and ftp should be auto-linked, too:
            http://google.com.
            """
            return x + y

        def annotation(x: int):
            """ Use function annotations. """
            return x

        class ClassWithAnnotation:
            def method_annotation(self, x: bytes):
                return x.decode()

        serv.register_function(add)
        serv.register_function(lambda x, y: x - y)
        serv.register_function(annotation)
        serv.register_instance(ClassWithAnnotation())

        while numrequests > 0:
            serv.handle_request()
            numrequests -= 1
    except socket.timeout:
        pass
    finally:
        serv.server_close()
        PORT = None
        evt.set()
def server(evt, numrequests):
    serv = DocXMLRPCServer(("localhost", 0), logRequests=False)

    try:
        global PORT
        PORT = serv.socket.getsockname()[1]

        # Add some documentation
        serv.set_server_title("DocXMLRPCServer Test Documentation")
        serv.set_server_name("DocXMLRPCServer Test Docs")
        serv.set_server_documentation(
            "This is an XML-RPC server's documentation, but the server "
            "can be used by POSTing to /RPC2. Try self.add, too.")

        # Create and register classes and functions
        class TestClass(object):
            def test_method(self, arg):
                """Test method's docs. This method truly does very little."""
                self.arg = arg

        serv.register_introspection_functions()
        serv.register_instance(TestClass())

        def add(x, y):
            """Add two instances together. This follows PEP008, but has nothing
            to do with RFC1952. Case should matter: pEp008 and rFC1952.  Things
            that start with http and ftp should be auto-linked, too:
            http://google.com.
            """
            return x + y

        def annotation(x: int):
            """ Use function annotations. """
            return x

        class ClassWithAnnotation:
            def method_annotation(self, x: bytes):
                return x.decode()

        serv.register_function(add)
        serv.register_function(lambda x, y: x-y)
        serv.register_function(annotation)
        serv.register_instance(ClassWithAnnotation())

        while numrequests > 0:
            serv.handle_request()
            numrequests -= 1
    except socket.timeout:
        pass
    finally:
        serv.server_close()
        PORT = None
        evt.set()
示例#3
0
def simple_server():
    def is_even(n):
        """
        偶数判断
        """
        return n % 2 == 0

    server = DocXMLRPCServer(("localhost", 8000))
    server.register_function(pow)
    server.register_function(lambda x, y: x + y, 'add')
    server.register_function(is_even, "is_even")
    server.register_introspection_functions()
    server.serve_forever()
def docxmlrpcserver(title, name, documentation):
    p = int(os.environ['INITIAL_PORT']
            )  # avoid starting the server on the same port by PyExZ3
    serv = DocXMLRPCServer(("localhost", p), logRequests=False)
    serv.set_server_title(title)  #"DocXMLRPCServer Test Documentation")
    serv.set_server_name(name)  #"DocXMLRPCServer Test Docs")
    serv.set_server_documentation(
        documentation)  #"This is an XML-RPC server's documentation")
    serv.register_introspection_functions()
    serv.register_multicall_functions()
    serv.register_function(lambda x, y: x + y)
    serv.register_instance(DocXMLRPCServer(("localhost", p + 1)))
    generated = serv.generate_html_documentation()
    os.environ["INITIAL_PORT"] = str(p + 2)
    if '<script>' in generated:
        return 'dangerous'
    else:
        return 'safe'
示例#5
0
def make_server():
    serv = DocXMLRPCServer(('localhost', 0), logRequests=False)
    try:
        serv.set_server_title('DocXMLRPCServer Test Documentation')
        serv.set_server_name('DocXMLRPCServer Test Docs')
        serv.set_server_documentation(
            "This is an XML-RPC server's documentation, but the server can be used by POSTing to /RPC2. Try self.add, too."
        )

        class TestClass(object):
            def test_method(self, arg):
                """Test method's docs. This method truly does very little."""
                self.arg = arg

        serv.register_introspection_functions()
        serv.register_instance(TestClass())

        def add(x, y):
            """Add two instances together. This follows PEP008, but has nothing
            to do with RFC1952. Case should matter: pEp008 and rFC1952.  Things
            that start with http and ftp should be auto-linked, too:
            http://google.com.
            """
            return x + y

        def annotation(x: int):
            """ Use function annotations. """
            return x

        class ClassWithAnnotation:
            def method_annotation(self, x: bytes):
                return x.decode()

        serv.register_function(add)
        serv.register_function(lambda x, y: x - y)
        serv.register_function(annotation)
        serv.register_instance(ClassWithAnnotation())
        return serv
    except:
        serv.server_close()
        raise
示例#6
0
class XMLRPC_Server(threading.Thread):
    def __init__(self, relays, stream, sensors, leds):
        self.relays = relays
        self.stream = stream
        self.sensors = sensors
        self.leds = leds
        threading.Thread.__init__(self)
        self.start()

    def stop(self):
        self.server.shutdown()
        self.server.server_close()

    def run(self):
        class RequestHandler(DocXMLRPCRequestHandler):

            rpc_paths = ('/AtlantisRPC', )

            def do_OPTIONS(self):
                self.send_response(200)
                self.end_headers()

            # Add these headers to all responses
            def end_headers(self):
                self.send_header(
                    "Access-Control-Allow-Headers",
                    "Origin, X-Requested-With, Content-Type, Accept")
                self.send_header("Access-Control-Allow-Origin", "*")
                DocXMLRPCRequestHandler.end_headers(self)

        self.server = DocXMLRPCServer(
            (GlobalConfig.xmlrpc_host, GlobalConfig.xmlrpc_port),
            requestHandler=RequestHandler)
        self.server.set_server_title("Atlantis RPCXML server")
        self.server.set_server_name("Atlantis RPCXML server")
        self.server.set_server_documentation(
            "Atlantis RPCXML server documentation")
        self.server.register_introspection_functions()
        self.server.register_function(self.get_status)
        self.server.register_function(self.set_device)
        self.server.register_function(self.turn_on_device)
        self.server.register_function(self.turn_off_device)
        self.server.register_function(self.toggle_device)
        self.server.register_function(self.turn_on_stream)
        self.server.register_function(self.turn_off_stream)
        self.server.register_function(self.toggle_stream)
        self.server.register_function(self.set_leds_color)
        self.server.serve_forever()

    # XMLRPC methods

    def get_status(self):
        status = {
            "sensors": self.sensors.getSensorsJSON(),
            "leds": [{
                "mode": "static",
                "r": 1.0,
                "g": 1.0,
                "b": 1.0
            }],
            "relays": {},
            "streaming": self.stream.isOn()
        }
        # Insert relay status
        for r in self.relays.devices:
            status["relays"][r] = self.relays.getDeviceState(r)
        return status

    def set_device(self, id, value):
        if value:
            self.relays.turnOnDevice(id)
        else:
            self.relays.turnOffDevice(id)
        return self.get_status()

    def turn_on_device(self, id):
        self.relays.turnOnDevice(id)
        return self.get_status()

    def turn_off_device(self, id):
        self.relays.turnOffDevice(id)
        return self.get_status()

    def toggle_device(self, id):
        self.relays.toggleDevice(id)
        return self.get_status()

    def turn_on_stream(self):
        self.stream.start()

    def turn_off_stream(self):
        self.stream.stop()

    def toggle_stream(self):
        if self.stream.isOn():
            self.stream.stop()
        else:
            self.stream.start()

    def set_leds_color(self, id, color):
        print("Setting LEDS color ", color)
        self.leds.setColor(color)
示例#7
0
# Implements a simple XMLRPC server for test purposes
# 26.03.2010 Bram

from xmlrpc.server import DocXMLRPCServer
from xmlrpc.server import DocXMLRPCRequestHandler


class RequestHandler(DocXMLRPCRequestHandler):
    rpc_paths = ('/RPC2', )


server = DocXMLRPCServer(("0.0.0.0", 2720), requestHandler=RequestHandler)

# gives access to system.listMethods, system.methodHelp and system.methodSignature
server.register_introspection_functions()


def reverse(x):
    return "".join(list(reversed(x)))


def get():
    return ["Power", "Energy", "Force", "Distance", "Current", "Apple pies"]


print("Registering functions")
server.register_function(reverse, 'reverse')
server.register_function(get, 'get')

print("Ready")