You can check if the call to http://docs.python.org/2/library/socket.html#socket.create_connection does throw an exception.