我正在尝试将其从 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")