Python Port Scanner

April 4, 2018 | Views: 5361

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

The following python code will allow you to scan a local or remote host for open ports. The program scans for select ports on a certain IP address and reflects the open ports back to the user.


#import socket library

from socket import *


#User prompts for IP and Ports

print “This tool should be used to verify open ports on the machine n”

print “Please follow the steps provided to check ports. n”

ip=raw_input(“Enter the IP of the machine that you would like to scan: “) #prompts user to enter in the pema ip

print “n”

print “-” * 60

print “Please enter the range of ports you would like to scan on the machine”

print “-” * 60

sport=input(“Enter first port number to scan: “)

print “n”

endport=input(“Enter last port number to scan:”)

print “n”


# Banner

print “-” * 60

print “Please wait, scanning remote host”, ip

print “-” * 60

print “All connected ports between “, sport, ” and “, endport, ” will display”

print “-” * 60

print “Press Ctrl + C to stop scan”

print “-” * 60


#Informs user of successful connections

for port in range(sport,endport,):


if(s.connect_ex((ip, port))==0):

print “Port” , port, “is open on PEMA ” + pema_ip



Thanks for looking!

Share with Friends
Use Cybytes and
Tip the Author!
Share with Friends
Ready to share your knowledge and expertise?
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge



Is Linux Worth Learning in 2020?
Views: 740 / December 14, 2019
How do I Get MTA Certified?
Views: 1312 / December 12, 2019
How much does your PAM software really cost?
Views: 1749 / December 10, 2019
How Do I Get into Android Development?
Views: 2139 / December 8, 2019

We recommend always using caution when following any link

Are you sure you want to continue?