Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does this code work?
#1
So I would consider myself just above a newbie. I know enough to get myself into trouble.

Anyways, I was looking into messing around with threading today and with the assistance of a youtube video built a multi-threaded port scanner. Everything works great but I am having trouble wrapping my head around some of the code. Here is the code in it's entirety:

import threading
import socket
from queue import Queue
import time

print_lock = threading.Lock()

target = input("What is the IP Address/Host you wish to scan?")
port_start = input("What is the start of the port range?")
port_end = input("What is the end of the port range?")

def portscan(port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        con = s.connect((target, port))
        with print_lock:
            print('port:', port, 'is open!')
        con.close()
    except:
        pass

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

q = Queue()

start = time.time()

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

for worker in range(int(port_start), int(port_end)):
    q.put(worker)

q.join()

print('Entire operation took ', time.time()-start, ' seconds.')
So, my confusion centers around the two for loops. Logically, I would think that the 'for worker' loop would need to occur first because that is the one that puts data into the queue. However, that doesn't seem to be the case, the program runs just fine the way it is. Can someone explain what I am missing here?
Reply
#2
I would suggest watching this video: https://www.youtube.com/watch?v=MCs5OvhV9S4
(code here): https://github.com/dabeaz/concurrencylive
Even though this video is three years old, it explains threading (concurrency) very well.
If that sparks your interest, you may want to watch the latest: https://www.youtube.com/watch?v=U66KuyD3...e=youtu.be
Don't watch the second without first watching the first, as the new video is on advanced features that are not yet part of python, but I expect will be soon.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 783 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 689 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Beginner: Code not work when longer list raiviscoding 2 821 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,792 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,444 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 891 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,246 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  cannot get code to work Led_Zeppelin 10 2,454 Jun-30-2022, 06:28 PM
Last Post: deanhystad
  How does this code work? pd_minh12 3 1,350 Apr-15-2022, 02:50 AM
Last Post: Pedroski55
  What should i do, for this code to work -> description hamad 2 1,464 Nov-18-2021, 01:22 PM
Last Post: ghoul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020