Python-based socket programming-------TCP-based sockets for remote execution of commands

Source: Internet
Author: User

Remote implementation of the CMD function:

ImportSocketImportSubprocessphone=Socket.socket (Socket.af_inet,socket. Sock_stream) Phone.bind (("127.0.0.1", 8080)) Phone.listen (5)Print("starting ....") whiletrue:conn,addr=phone.accept () whileTrue:Try: Date=CONN.RECV (1024) s=date.decode ("UTF8") Res=subprocess. Popen ("%s"% (s), shell=true,stdout=subprocess. Pipe,stderr=subprocess. PIPE,)ifRes.stdout:re1=Res.stdout.read () conn.send (Re1)ifRes.stderr:re2=Res.stderr.read () conn.send (Re2)exceptException: Breakconn.close () phone.close ( )
the server that actually executes the command
ImportSocketphone=Socket.socket (Socket.af_inet,socket. Sock_stream) Phone.connect (("127.0.0.1", 8080)) whiletrue:msg=input ("Please enter your cmd command:")    if  notMsgContinuephone.send (Msg.encode ("UTF8")) Data=PHONE.RECV (1024)    Print(Data.decode ("GBK") ) Phone.close ()
Remote Control Client

What's wrong with the feeling? )

Python-based socket programming-------TCP-based sockets for remote execution of commands

Related Article

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.