<p>This question will expand on: <a href="https://stackoverflow.com/questions/68774/best-way-to-open-a-socket-in-python">Best way to open a socket in Python</a><br />
When opening a socket how can I test to see if it has been established, and that it did not timeout, or generally fail.
<br /><br />
Edit:
I tried this:</p>

<pre><code>try:
    s.connect((address, '80'))
except:
    alert('failed' + address, 'down')
</code></pre>

<p>but the alert function is called even when that connection should have worked.</p>

It seems that you catch not the exception you wanna catch out there :)
if the
```
s
```
is a
```
socket.socket()
```
object, then the right way to call
```
.connect
```
would be:
```
```
import socket
s = socket.socket()
address = '127.0.0.1'
port = 80  # port number is a number, not string
try:
    s.connect((address, port)) 
    # originally, it was 
    # except Exception, e: 
    # but this syntax is not supported anymore. 
except Exception as e: 
    print("something's wrong with %s:%d. Exception is %s" % (address, port, e))
finally:
    s.close()
```
```
Always try to see what kind of exception is what you're catching in a try-except loop.
You can check what types of exceptions in a socket module represent what kind of errors (timeout, unable to resolve address, etc) and make separate
```
except
```
statement for each one of them - this way you'll be able to react differently for different kind of problems.