我正在尝试将此从Ruby转换为Python。 Ruby代码:Python套接字超时和刷新
def read_byte
begin
Timeout.timeout(0.5) do
b = socket.read 1
end
rescue Timeout::Error => e
socket.write("\n")
socket.flush
retry
end
end
def socket
@socket ||= TCPSocket.open @host, @port
rescue SocketError
# TODO: raise a specific error
raise "Unable to open connection to #{@host} with given parameters"
end
我这里指的问题是与
socket.flush
我不能找到一种方法做齐平。我可以用其他方式做到这一点? 我写了这个。 Python代码:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.host, self.port))
s.settimeout(0.5)
while True:
try:
print s.recv(1)
except socket.timeout:
s.sendall("\n")
你会期望冲洗套接字来执行'sendall()'dosen't已经做了什么? – kindall
流是有点挂在我的代码。它正在使用Ruby代码。 –