Categories: Linux

Testing if socket is accepting connections

nmap and netcat are great and easy ways to test if a socket is open and accepting connections:

Using netcat

 nc -zv 80
Connection to 80 port [tcp/http] succeeded!

Using nmap

 nmap -v -Pn -p 80
Starting Nmap 7.80 ( ) at 2021-03-02 06:23 CST
Initiating Parallel DNS resolution of 1 host. at 06:23
Completed Parallel DNS resolution of 1 host. at 06:23, 0.17s elapsed
Initiating Connect Scan at 06:23
Scanning ( [1 port]
Discovered open port 80/tcp on
Completed Connect Scan at 06:23, 0.06s elapsed (1 total ports)
Nmap scan report for (
Host is up (0.062s latency).
Other addresses for (not scanned): 2606:2800:220:1:248:1893:25c8:1946

80/tcp open  http

Read data files from: /usr/bin/../share/nmap
Nmap done: 1 IP address (1 host up) scanned in 0.36 seconds

ncat and nmap may not always be installed, also the telnet package is usually no longer installed by default in most distributions.

These are some simple alternatives I have found useful to test connectivity:

Using openssl, the timeout is long and not configurable, but it does clearly state when able to open a socket with a message saying CONNECTED

openssl s_client -connect
139935405417792:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../ssl/record/ssl3_record.c:331:
no peer certificate available
No client certificate CA names sent
SSL handshake has read 5 bytes and written 306 bytes
Verification: OK
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 0 (ok)

You can also use the following simple python2 script to test if a port is accepting connection, the script takes two arguments host and port.

import socket
import sys

def test_socket(ip,port):
    s = socket.socket()
    except socket.error as msg:
        print 'could not open %s:%s %s' % (ip,port,msg)
        print '%s:%s is OK' % (ip,port)


The script returns OK if its able to accept a connection

python 80 is OK

Or returns the socket.error instead

python 81
could not open timed out
 python localhost 81
could not open localhost:81 [Errno 111] Connection refused

Categories: Python

Python vimrc

A nice tip if you work with python under vi, use this vimrc config file.

you can just copy it to your home directory
wget ~/python_vimrc

you can start vi with vi -u ~/python_vimrc

Or make it your default .vimrc you will end with nice syntax highlighting