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 and the port being tested is 8765.

Create 3 files:
  • (a simple HTML server)
  • (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

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

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

# script name:
# import required modules
import SimpleHTTPServer, SocketServer, sys

# default port in case none specified on command line

# 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

    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

# script name:
# import required modules
import sys, urllib2

# define an url to match the address and port used by

# check for command line arguments and print usage info if none provided
if len(sys.argv) > 1 :
    URL = sys.argv[1]
    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 =
    if not len(data):

Put in same directory as
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.