server端:
import socket
import struct
import subprocess
IP = '127.0.0.1'
PORT = 8989
def get_cmd():
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sk.bind((IP,PORT))
sk.listen(5)
while 1:
connt,addr = sk.accept()
while 1:
cmd = connt.recv(1024)
res = subprocess.getoutput(cmd.decode())
print(res)
res_len = struct.pack('i',len(res))
connt.send(res_len)
connt.send(res.encode())
if __name__ == '__main__':
get_cmd()
client端:
import socket
import struct
IP = '127.0.0.1'
PORT = 8989
def get_cmd():
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sk.connect((IP,PORT))
while 1:
date = input('>>>: ').encode()
sk.send(date)
res_len = struct.unpack('i',sk.recv(4))[0]
recv_len = 0
res = b''
while recv_len < res_len:
date = sk.recv(1024)
res += date
recv_len += len(date)
print(res.decode())
if __name__ == '__main__':
get_cmd()