I'm having trouble using select. I just want a mean to know which clients are still there to receive data. There is my code :
import socket, select
server = socket.socket()
server.bind(('localhost',80))
server.listen(1)
answer = "HTTP/1.1 200 OK\r\n"
answer+= "Content-type: text/plain\r\n"
answer+= "Connection: close\r\n"
body = "test msg"
answer+= "Content-length: %d\r\n\r\n" % len(body)
answer+= body
clients = []
while True:
nextclient,addr = server.accept()
clients.append(nextclient)
clients = select.select([],clients,[],0.0)[1]
for client in clients:
client.send(answer)
The select send me everytime all the sockets opened before, even if the connection was closed on the other end, this results in a Errno1053 : an etablished connection was aborted by the software in your host machine.
I thank you in advance for your help.
Your select never blocks.
A time-out value of zero specifies a poll and never blocks.
Also, your listen
method's argument is absolutely extreme.
socket.listen(backlog)
Listen for connections made to the socket. The backlog argument specifies the maximum number of queued connections and should be at least 0; the maximum value is system-dependent (usually 5)
As far as I can tell, you never close a socket after writing to it and you don't as well remove it from clients
.
Besides, you overwrite clients
so that your list of clients is lost; some clients will never be processed.
Something like
clients_now = select.select([],clients,[],0.0)[1]
for client in clients_now:
client.send(answer)
client.close()
clients.remove(client)
might help.
BTW, just a small block of 1 or 10 ms will keep your server responsive, but prevents a high CPU load because of idle waiting.
BTW2: Maybe you should include your server socket in the select process as well...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.