How to check if a server is up or not in Python?
<p>In PHP I just did: <code>$fp = @fsockopen(irc.myserver.net, 6667, $errno, $errstr, 2);</code></p>

<p>Does Python 2.X also have a function like PHP's <code>fsockopen()</code>? If not how else can I check if a server on port 6667 is up or not?</p>

<p>The <a href="https://docs.python.org/2/library/socket.html" rel="nofollow">socket module</a> can be used to simply check if a port is open or not.</p>

<pre><code>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"
</code></pre>

python python-2.7