Python Remote Call Script RPC

Source: Internet
Author: User
Tags install openssl

Recently there is a monitoring requirement, you need to execute the script on each node of the cluster remotely, and get the result of the script execution, in order to be safe, do not need the account password to log in the host, require only the method that calls the remote script module can be implemented.

Summarize the Python script method for remote invocation:

    • Login host execution script, Python module support such as PSSH, Pexpect, Paramiko

    • Remote method calls (no login hosts required), Python module RPYC, support for distributed

    • Socket mode, slightly complicated, need to be familiar with network protocol, starting point is higher

RPYC support Remote Call, distributed computing, with less code to achieve the need for complex socket programming, this article mainly introduces RPYC and use it to implement a demo.

Introduced in code:

Requirements: Execute the server-side script on each node of the cluster separately and return the execution results to the client side

#Monitor_RPC_Client. py...hostdict = {' *.*.*.189 ': 12345,  ' *.*.*. 188 ': 12345,  ' *.*.*. 187 ':  12345}class processworker (Thread):     def __init__ (Self, queue):         thread.__init__ (self)          self.queue = queue    def run (self):         while True:             host, port, localpath = self.queue.get ()              try:                 c = rpyc.connect (Host, port)                  localfilesize = c.root.getlocalfilesize ( Localpath, glob30minago)                 localfilesizedict[host] = localfilesize                 c.close ()              except Exception, e:                 print  ' {0} → {1}:{2} occur a error:{3}\n '. Format ( Self.getname (),  host, port, e) def getlocalsize (localPath):     queue  = queue ()     for x in range (3):         worker = processworker (queue)          Worker.daemon = true        worker.start ()      for  (Host, port)  in&nbsP;hostdict.items ():         queue.put ((Host, port, localpath) )     time.sleep (3)     # queue.join () ... #Monitor_RPC_Server. Py ... Datepattern = re.compile (R ' \d{4} (?:-\ D{2}) {4} ') Def get_ip_address (ifname):     s = socket.socket (Socket.AF_INET,  socket. SOCK_DGRAM)     return socket.inet_ntoa (Fcntl.ioctl (         s.fileno (),        0x8915,  #  Siocgifaddr        struct.pack (' 256s ',  ifname[:15])      ) [20:24]) Class remote_call_func (Service):     def on_connect (self):         print  "[{0}]\t--------------<<< on_connect] . Format (Getnowtime ())     def on_disconnect (self): &NBsp;       print  "[{0}]\t-------------->>> on_disconnect" . Format (Getnowtime ())     def exposed_getlocalfilesize (self, path,  Glob30minago):        exitcode, execresult =  Commands.getstatusoutput ("ls -lrt "  + path +  "' |awk  ' {s+=$5}end{print  s} ' ")         dateTime30minAgo =  (Datetime.datetime.now ()  - datetime.timedelta (minutes=30)). Strftime ("%y-%m-%d %h:%m:%s")          print  "[{0}] → {1} → {2}". Format (datetime30minago, path,  Format (execresult,  ', '))         return execresultrpycserver  = threadedserver (remote_call_func, hostname=get_ip_address (' eth0 '),  port=12345, auto _register=false) Rpycserver.start ()

There are many similar examples in the official documentation, which are not covered in detail and should be noted in two points:

    • The server-side definition method needs to be called by the client and must define a method that starts with exposed, otherwise it will error Attributeerror: ' Remote_call_script ' object has no attribute ' Exposed_ Iamshell '

    • Server side default does not have authentication mechanism, if need authentication has recommended two ways: Threadedserver authenticator parameter and SSL module

    • Pip install RPYC, if import rpyc error, yum install Openssl-devel, and then recompile, install Python

Of course, there are many exceptions to consider, such as timeouts, validation failures, and so on.

Refer:

[1] Python Remote Call script (i)

http://www.dbunix.com/?p=3262

Http://rpyc.readthedocs.org/en/latest/tutorial.html

[2] Python learns to execute shell commands in--python

http://zhou123.blog.51cto.com/4355617/1312791

Python Remote Call Script RPC

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.