tcp remote control command output

# Server

import socket
import subprocess

# 开机 
phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM)

# Bind ip, port 
phone.bind (( ' 127.0.0.1 ' , 8080 ))

# Monitor 
phone.listen (5 )

# Links cycle 
the while . 1 :
     # receiving a maximum 1024 bytes 
    Conn, addr = phone.accept (1024 )

    # Communications cycle 
    the while . 1 :
         # exception handling 
        the try :
            data = conn.recv(1024)

            # Unix cannot receive empty 
            if len (data) == 0: break

            res = subprocess.Popen (bata.decode ( ' gbk ' ), (windows default character encoding 'gbk')      
            shell=True, stderr=subprocess.PIPE, 
            stdout=subprocess.PIPE)

            resout = res.stdout.read ()
            reserr = res.stderr.read ()

         # windows receive empty exit    
         except Exception:
             break
    
    conn.close()

phone.close() 
# Client

import socket

phone = socket.socket (socket.AF_INET, socket.SCOK_STREAM)

# Connection 
phone.connect (( ' 127.0.0.1 ' , 8080 ))

# Cycle output 
the while 1 :
    msg = input ( ' Please enter the command >>> ' ) .strip ()

    # You can not enter an empty 
    IF len (msg) == 0: the Continue

    # Send message 
    phone.send (msg.encode ( ' GKB ' ))
    
    data = phone.recv(1024)

    print(data.decode('gbk'))

# Close 
phone.close    

Run the server, and then run the client (of course this is to play with yourself ...), enter a dir command to try:

                    

 

 Well, that's it. .

 

Guess you like

Origin www.cnblogs.com/qjk95/p/12738734.html