[python]指定 Socket recv 方法的超时时间

使用 select 来变相实现超时功能:

import select
import socket

HOST = '127.0.0.1'
PORT = 8000
timeout = 60 * 1   # 1 分钟

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('msg')
# 设置 recv 超时时间
s.setblocking(0)
ready = select.select([s], [], [], timeout)
if ready[0]:
    # 接收结果
    data = s.recv(1024).strip('\x00')

参考


Comments