Python-RPC simple implementation

Source: Internet
Author: User

Server implementation:

 

 
Import simplexmlrpcserver, osrunning = truefuns = ['add', 'minus', 'exit ', 'getcurrentlist', 'linuxcmd']
 
# New Code slow RPC calls # basehttprequesthandler calls the log_message method to record some log information each time it returns, while the log_message method needs to know the requested host. # The problem lies here. When there is no hostProgramThe gethostname () method of _ socket is called, but the host on the server is not configured. Import basehttpserverdef not_insane_address_string (Self): Host, Port = self. client_address [: 2] Return '% s (no getfqdn)' % host # used to call: socket. getfqdn (host) basehttpserver. basehttprequesthandler. address_string = \ not_insane_address_string
# End new codeclass exposefuns (object): def add (self, a, B): return a + B def minus (self, a, B ): return a-B def exit (Self): Global running = false return 'exit! 'Def getcurrentlist (Self): Return OS. getcwd () def listdir (self, DIR): Return OS. listdir (DIR) def linuxcmd (self, DIR): Return OS. popen (DIR ). read () def useage (Self): Return funs; server = simplexmlrpcserver. simplexmlrpcserver ("localhost", 8000) server. register_instance (exposefuns () # server. register_function (ADD) while running: Server. handle_request ()

Client implementation:

 

 

 
Import xmlrpclibserver = xmlrpclib. server ('HTTP: // 127.0.0.1: 8000 ') print server. add (5, 88) print server. minus (server. add (5, 88), 5) print server. finis ()

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.