0
我被困在一個我正在嘗試使用的項目上。現在我正試圖設置一個腳本(這一個)在本地機器上打開一個端口,這將使我能夠訪問它上面的運行命令。我正在使用Netcat連接到它。現在它可以讓我連接到netcat,然後我可以輸入1個命令,它會給我輸出和停止。當我在while循環之外移動「conn,addr = s.accept()」時,我能夠反覆運行命令,並且在某些情況下應該像它應該那樣工作。但是當我嘗試運行「cd ..」來返回一個目錄時,它不會讓我。我不確定我是否缺少一些東西。如何用套接字向Python服務器重複命令?
host = ''
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket has been created.")
try:
s.bind((host, port))
except socket.error:
print("Bind failed.")
sys.exit()
print("Socket bind complete.")
s.listen(5)
print("Socket is now listening.")
while 1:
conn, addr = s.accept()
print("Connected with {0}:{1}".format(addr[0], addr[1]))
data = conn.recv(1024)
string_data = data.decode('utf-8')
proc = subprocess.Popen(string_data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
stdout_value = proc.stdout.read() + proc.stderr.read()
conn.sendall(stdout_value)
#conn.close()
#s.close()
我想啓動一次shell並與之通話。但不知道如何做到這一點。任何可以讓我朝着正確方向的提示? – sqlsqlsql