Python Port Scanner Not Showing Open Ports

Viewed 15

I'm following a Python tut on writing a port scanner, it runs, but it seemed to skip over a port that should theoretically be open. I'm running a web browser so port 80 should be up, but when I ran it against my network it just skipped over it. Also tried it against 443, but it's not showing any HTTPS ports either.

import sys #allows us to enter cmd line arguments & other things
import socket #Sockets and the socket API are used to send messages across a network. They provide a form of inter-process communication (IPC).
from datetime import datetime
#next we need to define our target
if len(sys.argv) == 2:
    target = socket.gethostbyname(sys.argv[1])   #translate host name to IPV4
else:
    print (“invald amt of arguments.”)
    print (“syntax: python3 scanner.py <ip>”)
    sys.exit()
#add a pretty banner
print (“-” * 50)
print (“scanning target” + target)
print(“Time started: “ +str(datetime.now()))
print (“-” * 50)
try:
    for port in range (50,85):
        s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
        socket.setdefaulttimeout(1) #is a float
        result = s.connect_ex((target,port)) #returns error indicator
        print ((“checking port {}”).format(port)) #returns error indicator
        if result ==0:
        print (“port {} is open”.format(port))
        s.close()
except KeyboardInterrupt:
    (“\Exiting Program”)
    sys.exit()
except socket.gaierror:
    print (“host name could not be resolved”)
    sys.exit()
except socket.error:
    print (“could not connect to server”)
    sys.exit()**
1 Answers

If You replace all smart quoutes with straight quoutes, indent the TRUE-block of the if-statement inside the for-loop and remove the escape character ("\") in the exception handler, then Your code runs fine.

Related