Python несколько клиентов меняют каталог сервера отдельно? - PullRequest
0 голосов
/ 28 марта 2020

Я хочу отправить команду "cd xx" с нескольких клиентов на сервер и изменить каталог на сервере. Теперь я могу отправить эту команду и изменить каталог, но если я пытаюсь использовать несколько клиентов, когда я меняю каталог с первого клиента, каталог также меняется во втором клиенте.

Например, я хочу сделать так:

First client : cd /a/b
Second client : cd /c/d
First client : pwd
 >> /a/b
Second client : pwd
 >> /c/d

код моего сервера:

def threaded(c):

    while True:

        data = c.recv(1024)

        if not data:
            print('Client has been disconnected.')
            break
        try:
            if (data.decode("utf-8"))[:3] == "cd ":
                 os.chdir(os.path.abspath(data[3:]))
                 data_o = b'changed'
            else:   
                data_o = subprocess.check_output(data, shell=True)

        except subprocess.CalledProcessError as e:
            c.send(b'failed\n')
            print(e.output)

        if(len(data_o) > 0):
            c.send(data_o)
        else:
            c.send(b'There is no terminal output.')

    # connection closed
    c.close()


def Main():
    host = ""

    port = 21111
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host, port))
    print("Connected to port : ", port)
    # put the socket into listening mode
    s.listen(5)
    print("Server is ready and socket is listening")


# a forever loop until client wants to exit
    while True:

        # establish connection with client
        c, addr = s.accept()
        print('Connected to :', addr[0], ':', addr[1])

        # Start a new thread and return its identifier
        start_new_thread(threaded, (c,))
    s.close()

код моего клиента:

def Main():
    host = '127.0.0.1'
    port = 21111

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    s.connect((host, port))

    message = input("Enter Command : ")
    while True:
     # message sent to server

        s.send(message.encode('ascii'))

        data = s.recv(1024)

        print(len(data))

        print('Received from the server :', str(data.decode('ascii')))

        ans = input('\nEnter a new command or press (n) :') 
        if ans == 'n':
            #message = input("enter message")
            break
        else: 
            message = ans
            continue

    # close the connection
    s.close()

Как я могу это сделать ? Спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...