(PYTHON简单服务器)输入任务列表与子进程发生冲突
我正在分析和编辑一些代码,但无法使其正常工作。问题在于,当cmd接收并发回命令tasklist的输出时,它只给出该输出。例如:当我发送命令:tasklist时,客户端从服务器的tasklist返回tasklist的结果 但是,如果您继续在程序中使用该命令,并再次使用该命令,那么这次它将发送任务列表代码,而不是dir的输出。当您使用dir和其他小功能时,当您反复使用命令而没有向客户机发送错误的输出时,它确实起作用。我怀疑这与大小和子流程有关(因为任务列表是一个大列表,而目录不是) 客户:(PYTHON简单服务器)输入任务列表与子进程发生冲突,python,python-3.x,subprocess,Python,Python 3.x,Subprocess,我正在分析和编辑一些代码,但无法使其正常工作。问题在于,当cmd接收并发回命令tasklist的输出时,它只给出该输出。例如:当我发送命令:tasklist时,客户端从服务器的tasklist返回tasklist的结果 但是,如果您继续在程序中使用该命令,并再次使用该命令,那么这次它将发送任务列表代码,而不是dir的输出。当您使用dir和其他小功能时,当您反复使用命令而没有向客户机发送错误的输出时,它确实起作用。我怀疑这与大小和子流程有关(因为任务列表是一个大列表,而目录不是) 客户: (S
(SOCKET CODE, i dindnt import that would make this code way longer than it used to be, the problem isnt there)
message = 4
socket_server.send(message.encode()) #at client's computer chooses command
if message == '4':
cmd_com = input("Type cmd command ")#
socket_server.send(cmd_com.encode())
receiveid_message = socket_server.recv(4096).decode()
print(receiveid_message)
continue
服务器:
message = (conn.recv(1024)).decode()
print(message)
if message =='1' #RECEIVES FIRST MESSAGE FROM CLIENT
def cmd():
def shell():
output = subprocess.run(instruction, stdout=subprocess.PIPE, shell=True, stderr=subprocess.PIPE,# BUG LIGT AAN SUBPROCESS
stdin=subprocess.PIPE)
return output
import threading
print("getting instructions")
instruction = (conn.recv(1024)).decode() #RECEIVES SECOND MESSAGE FROM CLIENT
shel = threading.Thread(
target=shell) # Use of threading and a global variable to avoid hanging if command is too long to produce an output (probably a better way to do this)
shel._running = True
shel.start()
time.sleep(1)
shel._running = False
result = str(shell().stdout.decode('CP437')) # CP437 Decoding used for characters like " é " etc.
conn.send(result.encode()) # SEND RESULT TO SERVER