Python 3 Programming Tutorial – Threaded port scanner

Now that we’ve seen how to make a simple port scanner in Python 3, we’ve found that it is quite slow and cumbersome. So here, we tie in our knowledge of the threading module that we learned previously to dramatically improve our performance.

Sample code for this basics series:

Python 3 Programming tutorial Playlist:

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6


29 thoughts on “Python 3 Programming Tutorial – Threaded port scanner”

  1. When I run this port scanner against your website, I get no ports open from 1 to 100.  Why is this?  Did you close some ports on your server?

  2. +sentdex 
    Do you realize that s.connect() does not actually return a connection (or any other) object? The close method is defined on the socket itself. so it should be s.close(), alternatively you could use the with statement eg. with s: s.connect((server, port))

    P.S. it seems like you should close the socket prior to attempting to acquire the print_lock considering that it blocking could cause a delay in releasing the socket. Though probably an entirely unnecessary observation considering the number of available sockets and the low chance of the block being for any significant amount of time.

    P.P.S. Glad I found your videos 🙂

  3. Great job. I haven't looked up on ports yet, but it appears you have port 22 and 80 up on your site, and google has only 80 up

  4. I don't understand, why this port scanner will scan for open ports? Could you explain a bit why connecting to a random web-site reveals open ports?

  5. import socket
    import threading
    from queue import Queue

    print_lock = threading.Lock()

    target = ''

    def portscan(port):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            con = s.connect((targer,port))
            with print_lock:
                print('port',port,'is open!')

    def threader():
        while True:
            worker = q.get()

    q = Queue()

    for x in range(100):
        t = threading.Thread(target=threader)
        t.daemon = True

    for worker in range(1,40000):


  6. Quick question, if you were to implement this as a module the first thing to call would be the for x in range(whatever range ) — > t.start() section of the code right? so in essence starting the domino effect.

  7. Ports 1 through 1024 are reserved for system protocols on a your computer. That's why you only had two ports showing up as open.

  8. let's say I wanted to have a queue of threads for multiple definitions to run in, just waiting on standby. and when a definition is ran it grabs a unused thread, so not to interrupt another definition in the middle of running. then when nothing is running, your threads are on stand by again. basically how do I make your threader definition more dynamic, so I can throw a function at it instead of hard coding the function into it?

  9. bro im not sure i totally get what Queue means ..
    like the thing i dont get is how the are the threads know like whom shold take which port
    i tried my own version with out Queue [ coz as i was saying i didnt comprehend it and im not just gonna copy ur code..]
    and what i did was deviding the number of ports with the number of threads but and there is ..
    they dont run linearly .. i mean i[ it works but its not like 1,2,3,4,5,]
    coz one thread is going from 1-20 and another from 20-40

  10. i got up the point of test my port in windows. bij i got the following problem.
    Traceback (most recent call last):
    File "", line 25, in <module>
    File "", line 8, in main
    OSError: [WinError 10048] Elk socketadres (protocol/netwerkadres/poort) kan normaal slechts één keer worden gebruikt.

    Ques.: how can i get around this???

Leave a Reply