我需要在python的socket recv方法上設(shè)置超時(shí)。怎么做?
3 回答

BIG陽(yáng)
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果要實(shí)現(xiàn)服務(wù)器端,則要查找的超時(shí)是連接套接字的超時(shí),而不是主套接字的超時(shí)。換句話說(shuō),連接套接字對(duì)象還有另一個(gè)超時(shí),這是socket.accept()方法的輸出。因此:
sock.listen(1)
connection, client_address = sock.accept()
connection.settimeout(5) # This is the one that affects recv() method.
connection.gettimeout() # This should result 5
sock.gettimeout() # This outputs None when not set previously, if I remember correctly.
如果實(shí)現(xiàn)客戶端,那將很簡(jiǎn)單。
sock.connect(server_address)
sock.settimeout(3)
添加回答
舉報(bào)
0/150
提交
取消