Using Python To Test If A TCP Port Can Be Reached

Technical articles
Welcome page
Importing Modules
Much of Python's functionality is available only by using modules. This article shows how to import the module SimpleHTTPServer and then use it to confirm that no firewall rules or network ACLs are blocking a specifc port.

What is a Module?
A module is a file of Python code.

The simplest ways to import a module into your script are:
  • import
  • import , ,
  • from import *

Note that in the second example, several modules are installed on one line, with commas seperating the module names.

Click here for information on how to download and install modules. 


What Is Being Achieved In This Example
You may need to confirm that a port is reachable on a server. For example, you may plan to be installing an application that uses that port and you wanted to confirm that the port is not blocked by a firewall rule or a network ACL.

In this example, it the server is run on 10.0.0.218 and the port being tested is 8765.

Create 3 files:
  • test_port_server.py (a simple HTML server)
  • test_port_client.py (gets the file index.html from the web server)
  • index.html (what will be displayed when the two scripts run successfully, put in same directory as test_port_server.py)

To run the test, run on seperate computers:
  • $ ./test_port_server.py 8765
  • $ ./test_port_client.py http://10.0.0.218:8765
  • Note: assumes you are running the server on 10.0.218

You can test without test_port_client.py by using a browser, test_port_server.py and index.html. If connectivity exists, the browser will display the contents of index.html.


test_port_server.py

#!/path/python
# script name: test_port_server.py
# import required modules
import SimpleHTTPServer, SocketServer, sys

# default port in case none specified on command line
DEFAULT_PORT = 8765

# check for command line options
# use default port and print out usage if none provided
# use int function to make sure variable is treated like an integer
if len(sys.argv) > 1 :
    PORT = int(sys.argv[1])
    print "using port", PORT
else:
    PORT = int(DEFAULT_PORT)

    print "usage: ", sys.argv[0], "port number"
    print "using default port ", PORT
    print "use control-c to stop script \n"

# use HTTP server from module, indicate PORT to use
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)

# print out port number and start running HTTP server
print "serving at port", PORT
httpd.serve_forever()

test_port_client.py

#!/path/python
# script name: test_port_client.py
# import required modules
import sys, urllib2

# define an url to match the address and port used by test_port_server.py
DEFAULT_URL = "http://10.0.0.218:8765"

# check for command line arguments and print usage info if none provided
if len(sys.argv) > 1 :
    URL = sys.argv[1]
else:
    URL = DEFAULT_URL
    print "usage: ", sys.argv[0], "url"
    print "using default url ", URL

# Request and urlopen are part of the urllib2 module
req = urllib2.Request(URL)
fd = urllib2.urlopen(req)

# dump page to standard out
while 1:
    data = fd.read(1024)
    if not len(data):
        break
    sys.stdout.write(data)


index.html
Put in same directory as test_port_server.py
Test Page
You have reached the test server.



DNS bind name server

DNS bind name server

Related Articles




This article describes how to use Python to confirm that a TCP port is reachable. This document also shows how to import and use modules in Python.   Note that this code depends on Python 2.7.