我的代码有问题,我想通过插座在我的Virual机器上运行一个命令。我发现了多个任务,命令的问题。例如,如果我打开Excel,我的套接字服务器已经冻结,并且在我手动关闭Excel应用程序之前,我无法通过CMD运行另一个命令。
我应该更改代码以一次在我的虚拟机上打开多个应用程序?(例如。我想一次打开四个XLSX文件(
server.py
import socket
import subprocess
import os
from threading import Thread
def check_ping(hostname):
response = os.system("ping " + hostname)
# and then check the response...
if response == 0:
pingstatus = "Network Active"
else:
pingstatus = "Network Error"
return(pingstatus)
def run_path(path):
response = os.system(path)
# and then check the response..
return('ok')
def Main():
host = "localhost"
port = 5000
mySocket = socket.socket()
mySocket.bind((host,port))
mySocket.listen(1)
conn, addr = mySocket.accept()
print ("Connection from: " + str(addr))
while True:
data = conn.recv(1024).decode()
if not data:
break
#print ("from connected user: " + str(data))
#data = str(data)
#hostname = data.replace('ping ', '')
print ("sending: " + str(data))
#print(hostname)
conn.send(data.encode())
if 'ping' in str(data):
hostname = data.replace('ping ', '')
pingstatus = check_ping(hostname)
print(pingstatus)
elif 'open' in str(data):
path = str(data).replace('open ', '')
run_path(path)
conn.close()
if __name__ == '__main__':
Main()
client.py
import socket
def Main():
host = '127.0.0.1'
port = 5000
mySocket = socket.socket()
mySocket.connect((host,port))
message = input(" -> ")
while message != 'q':
mySocket.send(message.encode())
data = mySocket.recv(1024).decode()
print ('Received from server: ' + data)
message = input(" -> ")
mySocket.close()
if __name__ == '__main__':
Main()
te能够在上一个命令返回之前运行一个新命令,您只需要等待该事件即可。这意味着您应该使用子过程模块而不是system
函数:
def run_path(path):
response = subprocess.Process(path, shell=True)
# and then continue without waiting...
return('ok')
,但是如果您想将任何东西归还给同伴,那真的没有意义。此外,从远程客户端启动GUI程序而不能够控制它,也没有阻止它。
我怕你在这里追逐一个怪异的动物...