gpt4 book ai didi

python-3.x - 无法为python 3 recv设置超时

转载 作者:行者123 更新时间:2023-12-03 11:59:17 33 4
gpt4 key购买 nike

我正在尝试超时下面的代码。但是它只是卡在recv上,永远不会超时。有人可以指出我做错了什么吗?我看了一下,似乎找不到太多了。

import socket

host = "localhost"
port = 8888

# create socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# give object the ip and port to listen on
server_address = (host, port)
print('starting up on %s port %s' % server_address)
sock.bind(server_address)
# how many listeners
sock.listen(0)
# sets the time out
sock.settimeout(10)

while True:
print('waiting for a connection')
try:
#this waits for a connection from the sending side.
connection, client_address = sock.accept()
print('connection from', client_address)

start = False
message = ""
while client_address != "":
#this listens and waits for data to be sent and sets it to the data variable
data = connection.recv(32000).decode()

最佳答案

您已在监听套接字上设置了接受超时,而不是在接收套接字上设置了读取超时。

关于python-3.x - 无法为python 3 recv设置超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47038949/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com