Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Using the socket module in python 2.7:

1. I received a socket object from a socket.accept() method.
2. I checked the timeout of the socket object and it returned -1.0. I think this means the socket object is in blocking mode.
3. However when I call the recv method on the socket object I receive the error:
[Errno 10035] A non-blocking socket operation could not be completed immediately
4. This error only makes sense if the socket object is non blocking. The socket object should block until data arrives.

Why is the socket object non blocking when it should be blocking?

NOTE: I have set a timeout on the socket which called the accept() method. Could this be causing the returned socket object to be non blocking as well? If so, I would grateful to hear an explanation as to why this is happening.

Python
def wait_for_connection(self):
    connection, client_address = self.sock.accept()
    print 'connected to ' + str(client_address)
    self.connections.append(connection)


Python
def read_from_connections(self):
    for connection in self.connections:
        try:
            command = connection.recv(1024)
            if command:
                print command
        except socket.error, error:
            print error  # error 10035.
            print connection._sock.timeout  # returns -1
            if error.errno != errno.EWOULDBLOCK:
                raise



What I have tried:

I've tried looking for blocking_mode property somewhere inside the socket object using the built in dir() method, but I was not successful. So I used the timeout property as an indicator of whether the socket is blocking or non blocking. Please let me know if this assumption is wrong.
Posted
Updated 6-May-17 5:42am

1 solution

Rather than guessing, it is better to go to the documentation at Socket Programming HOWTO — Python 2.7.13 documentation[^] where it is clearly explained.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900