How to check if a server is up or not in Python?

python, python-2.7

Solution

The socket module can be used to simply check if a port is open or not.

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('irc.myserver.net', 6667))
if result == 0:
   print "Port is open"
else:
   print "Port is not open"

Problem

In PHP I just did: `$fp = @fsockopen(irc.myserver.net, 6667, $errno, $errstr, 2);` Does Python 2.X also have a function like PHP's `fsockopen()`? If not how else can I check if a server on port 6667 is up or not?

Original source

Related problems