Skip to content

Instantly share code, notes, and snippets.

@queercat
Created December 29, 2017 09:04
Show Gist options
  • Select an option

  • Save queercat/054a7a2f1955e9b4a7f2fd100bfa0e09 to your computer and use it in GitHub Desktop.

Select an option

Save queercat/054a7a2f1955e9b4a7f2fd100bfa0e09 to your computer and use it in GitHub Desktop.
A simple port scanner. Written in Python.
import argparse
import socket
parser = argparse.ArgumentParser(description='Query a host address of all ports, returns open or closed.')
parser.add_argument('-u', type=str, help='The host address (e.g.) example.com')
args = vars(parser.parse_args())
host = args['u']
for port in range(20, 1025):
s = socket.socket()
s.settimeout(.1) #Low as possible to still get a correct resp.
try:
s.connect((host, port)) #Try to connect.
except Exception:
s.close()
print 'Port [%d] is closed.' % (port)
continue #Do nothing!
else:
s.close()
print 'Port [%d] is open.' % (port) #Print the port works!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment