实现远程执行系统命令
服务端:
import socket
import os
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('127.0.0.1',8080))
s.listen(5)
data_coun,addr = s.accept()
print(data_coun)
while True:
try:
data1 = data_coun.recv(1024)
if not data1:break
print(data1.decode('utf-8'))
result = os.system(data1.decode('utf-8'))
if not result:
data_coun.send('命令已执行'.encode('utf-8'))
else:
data_coun.send('no cmd'.encode('utf-8'))
except Exception:
print('服务端:客户端断开了')
break
data_coun.close()
客户端:
import socket
c = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print(c)
c.connect(('127.0.0.1',8080))
print(c)
while True:
cmd = input('请输入命令:').strip()
if not cmd:continue
if cmd == 'q':break
c.send(cmd.encode('utf-8'))
data = c.recv(1024)
print(data.decode('utf-8'))
c.close()