multithreading - Python Socket causes accept to be called too many times -


i trying write simple multithreaded web server in python. when new client socket connects server socket, create new thread handle client socket, so:

(clientsock, (ip, port)) = self.sock.accept() newthread = clientthread(ip, port, clientsock) newthread.start() 

where clientthread class has run method, in turn calls following bit of code:

    data_string = ''     bytes = self.socket.recv(2046)     bytes_str = bytes.decode('utf-8')     data_string += bytes_str 

where self.socket clientsock referenced above.

this code works expected: 1 thread created when server socket accepts connection client socket, , receives client socket request.

but if change above code in order loop while there more data, such as:

    data_string = ''     bytes = self.socket.recv(2046)     while len(bytes) > 0:         bytes_str = bytes.decode('utf-8')         data_string += bytes_str         print 'data received: %s' % (bytes_str)         bytes = self.socket.recv(2046) 

then have strange issue. seems accept being called multiple times, thereby creating new, unnecessary thread each time.

i.e - if start client , try access file, following output server:

we have accepted connection (127.0.0.1, 59983) new thread started (127.0.0.1, 59983) have accepted connection (127.0.0.1, 59984) new thread started (127.0.0.1, 59984) 

why accept being called multiple times if 1 client has connected server?

edit expected behavior?


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -