A kiválasztott változat és az aktuális verzió közötti különbségek a következők.
Előző változat mindkét oldalon Előző változat Következő változat | Előző változat | ||
tanszek:oktatas:iss_t:xml-rpc [2023/03/19 14:34] knehez |
tanszek:oktatas:iss_t:xml-rpc [2024/01/12 10:30] (aktuális) knehez |
||
---|---|---|---|
Sor 1: | Sor 1: | ||
- | ===== XML-RPC ===== | + | ===== XML-RPC tutorial ===== |
- | XML-RPC is a popular integration method used in web development, especially in the context of web services. It enables developers to create web services that can be accessed by other applications and platforms using simple HTTP requests. | + | [[https://www.tutorialspoint.com/xml-rpc/xml_rpc_intro.htm|Introduction to XML-RPC]] |
+ | ===== Server ===== | ||
- | XML-RPC is a widely adopted integration method because it is platform-independent and can be used with any programming language that supports HTTP and XML. It is also a lightweight protocol that uses a small amount of bandwidth and can be easily implemented on both the client and server sides. | + | <sxh python> |
+ | from xmlrpc.server import SimpleXMLRPCServer | ||
+ | from xmlrpc.server import SimpleXMLRPCRequestHandler | ||
- | [[https://www.tutorialspoint.com/xml-rpc/xml_rpc_intro.htm|Introduction to XML-RPC]] | + | # Restrict to a particular path. |
+ | class RequestHandler(SimpleXMLRPCRequestHandler): | ||
+ | rpc_paths = ('/RPC2',) | ||
+ | |||
+ | # Create server | ||
+ | with SimpleXMLRPCServer(('localhost', 8000), | ||
+ | requestHandler=RequestHandler) as server: | ||
+ | server.register_introspection_functions() | ||
+ | |||
+ | # Register pow() function; this will use the value of | ||
+ | # pow.__name__ as the name, which is just 'pow'. | ||
+ | server.register_function(pow) | ||
+ | |||
+ | # Register a function under a different name | ||
+ | def adder_function(x, y): | ||
+ | return x + y | ||
+ | server.register_function(adder_function, 'add') | ||
+ | |||
+ | # Register an instance; all the methods of the instance are | ||
+ | # published as XML-RPC methods (in this case, just 'mul'). | ||
+ | class MyFuncs: | ||
+ | def mul(self, x, y): | ||
+ | return x * y | ||
+ | |||
+ | server.register_instance(MyFuncs()) | ||
+ | |||
+ | # Run the server's main loop | ||
+ | server.serve_forever() | ||
+ | </sxh> | ||
+ | |||
+ | ===== Client ===== | ||
+ | |||
+ | <sxh python> | ||
+ | import xmlrpc.client | ||
+ | |||
+ | s = xmlrpc.client.ServerProxy('http://localhost:8000') | ||
+ | print(s.pow(2,3)) # Returns 2**3 = 8 | ||
+ | print(s.add(2,3)) # Returns 5 | ||
+ | print(s.mul(5,2)) # Returns 5*2 = 10 | ||
+ | |||
+ | # Print list of available methods | ||
+ | print(s.system.listMethods()) | ||
+ | </sxh> |