2014-08-29 40 views
0

我正在用python寫一個遠程shell服務器。它的工作原理如下。在python中實現遠程shell服務器

1-客戶端連接到服務器。 2 - 服務器提示客戶端登錄。 2 - 之後,用戶可以發送一個shell命令給服務器。 3 - 服務器執行命令。

這裏是服務器代碼的片段,處理即將到來的請求

cmd=sock.recv(8192) 
output = subprocess.Popen(cmd.split(),stdout=subprocess.PIPE).stdout.read() 
sock.send(output) 

我的問題是我想要的服務器來處理多個客戶端,我想每一個客戶有一個單獨的shell會話。

編輯:

我希望服務器時,它接收來自客戶端的新連接啓動shell的新實例,並保持該實例在內存中,直到連接關閉。

+1

嘿那裏。它似乎並沒有真的問過一個問題:( – Ffisegydd 2014-08-29 09:31:50

回答

0

你可以實現它,爲每個連接的客戶端服務器啓動一個線程,只爲該客戶端並處理他的命令。例如:

def client_handler(socket, address): 
    # receive client commands and do whatever is necessary 

if __name__ == '__main__': 
    # Socket, Bind, Listen 
    while True: 
     # Wait for connection and accept 
     thread.start_new_thread(client_handler, (socket, address))