【发布时间】:2020-06-09 21:32:59
【问题描述】:
我有一个 TCP 客户端-服务器,客户端向服务器发送 shell 命令,服务器以所述命令的输出进行响应。某些命令(例如 date 或 cd )不起作用,因为我假设它们是交互式的,而我的代码无法处理它。每当我发送所述命令时,我的客户端似乎挂起,只是说发送......而我的服务器从未收到任何东西。
我想弄清楚如何在可能出现超时的情况下处理这个问题,如果发送命令的时间超过 5 秒,客户端将导致超时或简单地处理它并打印一条消息,说明命令没有在保持客户端连接到服务器的同时成功执行。
这是我的代码
客户:
import socket
# Client
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # creates TCP Socket
# local host and port
LOCAL_HOST = '127.0.0.1'
PORT = 5313
BUFFER_SIZE = 5000 # size of message
sock.settimeout(5)
# connect socket to ip and port
sock.connect((LOCAL_HOST, PORT))
print("Connected to server\n")
print("Enter quit to close connection\n")
while True:
message = input("Please enter a command:\n") # ask user to input message
if message == 'quit':
break
if len(message) == 0:
print("Please enter something")
message = input("Please enter a command:\n")
print("Sending %s" % message)
sock.send(str.encode(message)) # send message
command = str(sock.recv(BUFFER_SIZE), "utf-8") # receive message
print("received %s" % command)
print("closing connection with server")
sock.close()
服务器:
import socket
import subprocess
# Server
# Some commands do not work such as cd. Date does not work on windows because it is interactive.
# creates TCP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
# port and server ip(localhost)
LOCAL_HOST = ''
PORT = 5313
BUFFER_SIZE = 5000 # size of message
# test connection
print("Server starting up on %s with port number %s" % (LOCAL_HOST, PORT))
# bind socket to ip and port
sock.bind((LOCAL_HOST, PORT))
# listen to socket
sock.listen(1)
# socket will accept connection and client address
print("Waiting for connection") # waiting for connection
connection, address = sock.accept() # accept connection with client address
print("Connected to", address) # connected by address
while True:
command = connection.recv(BUFFER_SIZE) # receive message from client
if not command:
break
if len(command) > 0:
terminal = subprocess.Popen(command[:].decode("utf-8"), shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stdin=subprocess.PIPE)
output = terminal.stdout.read() + terminal.stderr.read()
output_as_string = str(output, "utf-8")
connection.send(str.encode("0:") + str.encode(output_as_string))
print(output_as_string)
print("Closing Server")
sock.close()
connection.close()
【问题讨论】:
标签: python sockets tcp subprocess client-server