服务端:
1 import socket 2 def connect(): 3 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 4 s.bind((‘192.168.1.105‘, 8000)) 5 s.listen(1) 6 print ‘Listen on: 8000‘ 7 conn, addr = s.accept() 8 print ‘Conn from ‘, addr 9 10 while True: 11 cmd = raw_input(‘cmd>‘) 12 if ‘exit‘ in cmd: 13 conn.send(‘exit‘) 14 conn.close() 15 break 16 else: 17 conn.send(cmd) 18 print conn.recv(1024) 19 20 connect()
客户端:
1 import socket,subprocess 2 c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 3 c.connect((‘192.168.1.105‘,8000)) 4 while True: 5 cmd = c.recv(1024) 6 if ‘exit‘ in cmd: 7 c.close() 8 break 9 else: 10 cmd_ = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) 11 c.send(cmd_.stdout.read()) 12 c.send(cmd_.stderr.read())
时间: 2024-10-13 23:09:34