简体   繁体   English

如何通过python套接字运行多个命令

[英]How to run multiple commands via python socket

I have problem with my code, I want to run a commands on my virual machines via sockets. 我的代码有问题,我想通过套接字在我的虚拟机上运行命令。 I found problem with multiple tasks,commands. 我发现了多个任务,命令的问题。 For example if I open Excel my socket server has been freezed and I cannot run another command via cmd before I close excel application manually. 例如,如果我打开Excel,我的套接字服务器已经冻结,在手动关闭excel应用程序之前,我无法通过cmd运行另一个命令。

What I should change in code to open multiple applications in one time on my virtual machine? 我应该在代码中更改我的虚拟机上一次打开多个应用程序的内容? (for example. I want to open in one time four xlsx files) (例如。我想一次打开四个xlsx文件)

Server.py 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 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 be able to run a new command before the previous one has returned, you have just not to wait for that event. 能够在前一个命令返回之前运行新命令,您只需要等待该事件即可。 That means that you should use the subprocess module instead of the system function: 这意味着您应该使用子进程模块而不是system函数:

def run_path(path):
    response = subprocess.Process(path, shell=True)
    # and then continue without waiting...
    return('ok')

But it does not really make sense if you want to return anything to the peer. 但是如果你想把任何东西归还给同伴,那真的没有意义。 In addition, it does not really make sense to launch a GUI program from a remote client without being able to control it, nor even to stop it. 此外,从远程客户端启动GUI程序而不能控制它甚至停止它都没有意义。

I am afraid you a chasing a weird animal here... 我恐怕你在这里追逐一种奇怪的动物......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM