033远程执行命令
程序员文章站
2022-03-28 19:46:08
在服务器执行命令之前好像缺了执行命令的模块,现在补上 ###远程执行命令例子: #指令执行结果短的,不会出现问题 1 import socket 2 import subprocess 3 def get_server_socket(): 4 sk = socket.socket() 5 serve ......
在服务器执行命令
之前好像缺了执行命令的模块,现在补上
import subprocess obj = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE) #拿到一个对象,前面是命令,shell设置可以执行脚本,stdout设置一个进程管道,这里实际上是有两个进程,一个是主进程,一个是shell进程 x = obj.stdout.read() # 这里拿到执行信息 print(str(x,'gbk')) # str(x,'utf8')忘记系统是gbk编码了
###远程执行命令例子:
#指令执行结果短的,不会出现问题
1 import socket 2 import subprocess 3 def get_server_socket(): 4 sk = socket.socket() 5 server_address = ('127.0.0.1',8888) 6 sk.bind(server_address) 7 sk.listen(5) 8 return sk 9 10 def get_conn(sk): 11 print('waitconnect...') 12 conn,addr = sk.accept() 13 return conn 14 15 if __name__ == '__main__': 16 sk = get_server_socket() 17 conn = get_conn(sk) 18 while True: 19 try: 20 data = conn.recv(1024)##Linux这里不会报错,如果强行关闭了conn,data就会变成空,也就是说Linux的直接不try就行了。 21 except Exception as e: 22 conn = get_conn(sk) 23 print(str(data,'utf8')) 24 if not data: 25 conn = get_conn(sk) 26 continue 27 sp = subprocess.Popen(str(data,'utf8'),shell=True,stdout=subprocess.PIPE)#注意命令要是字符串类型 28 result = sp.stdout.read()#str(sp.stdout.read(),'gbk') 29 conn.send(result) 30 print(str(result,'gbk')) 31 print('waiting...') 32 conn.close()
1 import socket 2 3 def connect_server(): 4 sk = socket.socket() 5 server_address = ('127.0.0.1',8888) 6 sk.connect(server_address) 7 return sk 8 9 if __name__ == '__main__': 10 sk = connect_server() 11 while True: 12 inp = input('>>>') 13 if inp == 'exit': 14 break 15 sk.send(bytes(inp,'utf8')) 16 print('waiting...') 17 data = sk.recv(1024) 18 print(str(data,'gbk')) 19 sk.close()
#指令执行结果较长,无法一次传递,改进。传送文件的思想。
1 import socket 2 import subprocess 3 def get_server_socket(): 4 sk = socket.socket() 5 server_address = ('127.0.0.1',8888) 6 sk.bind(server_address) 7 sk.listen(5) 8 return sk 9 def get_conn(sk): 10 print('waitconnect...') 11 conn, addr = sk.accept() 12 return conn 13 14 if __name__ == '__main__': 15 sk = get_server_socket() 16 conn = get_conn(sk) 17 while True: 18 try: 19 data = conn.recv(1024)##Linux这里不会报错,如果强行关闭了conn,data就会变成空,也就是说Linux的直接不try就行了。 20 except Exception as e: 21 conn = get_conn(sk) 22 print(str(data,'utf8')) 23 if not data: 24 conn = get_conn(sk) 25 continue 26 sp = subprocess.Popen(str(data,'utf8'),shell=True,stdout=subprocess.PIPE)#注意命令要是字符串类型 27 result = sp.stdout.read()#读取内容 28 conn.sendall(bytes(str(len(result)),'utf8'))#发送长度 29 conn.sendall(result)#发送内容 30 print(str(result,'gbk')) 31 print('waiting...') 32 conn.close()
1 import socket 2 def connect_server(): 3 sk = socket.socket() 4 server_address = ('127.0.0.1',8888) 5 sk.connect(server_address) 6 return sk 7 8 if __name__ == '__main__': 9 sk = connect_server() 10 while True: 11 inp = input('>>>') 12 if inp == '__exit': 13 break 14 sk.send(bytes(inp,'utf8')) 15 print('waiting...') 16 17 result_length = int(str(sk.recv(1024),'utf8'))#记录长度,然后下面接收到相应长度的那内容停止 18 data = bytes() 19 while len(data) != result_length: 20 r = sk.recv(1024) 21 data += r 22 print(str(data,'gbk')) 23 sk.close()
####另外两次连续接收会出现粘包现象
做个隔断,就是接收方随便发送一个数据,发送方接受后才继续发送,这样子就解决了。
上一篇: 条件选择