Python 3 Programming Tutorial - Threaded port scanner
Key Takeaways
This video tutorial demonstrates how to create a threaded port scanner in Python 3 using the threading module, significantly improving the performance of a simple port scanner by running multiple threads concurrently. The tutorial covers defining a port scanner function, creating threads, and managing a queue to assign ports to be scanned.
Full Transcript
hello everybody and welcome to another socket tutorial video uh this video is going to be kind of building on the last video that we were uh covering and that was a port scanner and actually I want to show you guys the combination of socket and the threading module which we've already covered the threading module um show you another example of threading and action so uh if you don't remember uh this was our threading uh tutorial here and the actual job that was being run was actually just ed. sleep 0.5 so it was just an operation of sleep um but as you can see this is very simple we have a function that's example job period um so in theory we should be able to just throw our P scanner right into example job and that would be it right so uh let me move that aside now and uh we're going to go ahead I think what we'll do is we'll we'll rewrite most of this just so we can go over everything one more time and really rehash uh what we've got going on so obviously we're going to import socket we're also going to import threading and then we're going to say from Q uh import q and then I don't really think we need to import time for any reason so we're not going to import uh time like we did before and now we're ready to go um we're going to use a print lock again so we'll say print lock equals threading do Capital lock and then we're going to say our Target equals again uh pythonprogramming.net and now ready to get started so just like before uh we need our Port scanner so I'm going to Define port scan uh and then the parameters we'll have Port um then uh we're going to say s equals socket. socket and then notet parentheses here socket. aore inet and then soet et. sock stream so again nothing new so far with sockets we've already covered uh what that line means and now we want to engage in a try and accept uh like we had before and so we're going to say try con for connection equals s. connect and then we want to connect to Target comma port in a nice little uh tupal here and so we're going to try to do that and then if that gets successful we're going to run this with statement and we're going to say with print lock uh [Music] print Port uh port is open okay so we do that um and then once we're done with that we're going to say con. close and because we want to close that connection once we're fin finally done um and then accept and then pass so if it doesn't work we're just going to pass we're not going to print out like oh this is uh not open or whatever because then we're just wasting our print statements with this print lock so that's it for our our Port scanner um it should look almost identical to the port scanner that we built in the previous video um so now uh we're going to Define our threader uh which really is going to be an exact copy of the previous threader that we had before so that's going to be wow true and it'll be worker equals q.g get and then port scan worker so not totally identical I suppose because this was like uh example job um port scan worker and then finally q.ask uncore done um just as a rehash this is going to get the worker from the que this is going to run the example job with the worker passing through as the port num so when we we specify a range of numbers as workers um so we can also specify them as ports why not and then when we're all done we say q.ask done to uh empty out their Q awesome so then we'll come down here and we had better defined Q so Q equals q u empty parms and now we're ready to uh create our workers so to speak how many threads are we going to run so for now uh we're going to say uh we're going to have we'll say we'll have 30 30 different workers so for X in range uh 30 so we've got 30 workers uh we're going to say t equals threading do thread Target equals oops threader so what's happening here threading do thread so we're creating a thread and then our Target for that thread is threader which does this gets uh workers from the que so we're using q and threading and uh we'll set them to work on Port scanning uh which is here which we've written again um all this stuff was covered in its own right so far we're just combining those two things so we do we did already cover a specific threading tutorial and we've already covered the port scanner um so sorry if we're moving too fast if we are check out the previous videos uh next t. d demon uh demon equals true uh because we want it to be a demon so it will die when the main thread dies and t. start which has to be called after uh demon now um we don't really need to be logging like we did before so we're not going to use the logging time um now we're going to assign the amount of jobs and so you can think of the amount of jobs as in the amount of ports right so we'll say four worker in range and then this time instead of just 30 or 100 or whatever we need to specify starting point which will be one and then we'll say 1 to 100 and one so we'll test the first 100 ports and the reason why we're doing we have to say one is because Port zero is an invalid Port we can't scan Port zero so anyway moving on um four worker in range one to 100 basically I don't know why it worked so finicky like that but basically it means it will scan Port one up to 101 so it really means Port one to 100 anyway moving along q.p put worker so we're putting that worker to work okay and then what we want to do is uh q. join so that we'll wait until the thread terminates and that really should be it so um hopefully if you walked through the threading tutorial with me you can see how we've basically written the same uh script as we did with our initial threading tutorial where uh the job was just to sleep we've basically replaced that with port scan and that's about it so we've just kind of meshed those together so that's kind of why I wanted to organize the threading tutorial in the way that I did because you just replace the function pretty much with whatever you need to replace it with um so yeah so anyway uh let's go ahead and save save and run this see if I uh made any errors here or if it worked out for us so save and run it's been popping up here yep es coock stream so let's fix that B to make some errors let's try again and immediately we see Port 22 is open does anybody remember how long it took to get Port 22 to show that it was open took a very long time um so that was actually very quick um compared to before we actually showed Port 22 and Port 80 uh well in advance probably we wouldn't even seen it with our original Port scanner yet so anyway that was very quick um then we could continue we could say hey we want to do 10 th000 jobs and we want to have 100 threads let's say um so we can save and run that and you can see 22 and 80 were open we we knew about their their being open immediately and we're going to continue running through ports um adventure to guess there are probably a few more ports that are open um how far did we go 10,000 maybe some in the 9000s will be open um not not quite sure really not sure what number we're on even right now um but we'll see anyway so now we're actually you know we're using 100 threads to port scan so as you can see we can we could use this to our advantage uh threading and be Port scanning you know a lot of ports at at any one time as compared to the really slow port scan before um but anyway um if this pops happens to pop up with some more ports great otherwise I'm going to start concluding this video um so that's it with this video in the next video uh we're going to be moving away from Port scanning I just wanted to show you some people uh Port scanning is kind of a a pretty popular topic and it involves sockets it's very basic so it's really easy to show plus I could mesh it with sockets and threading so I made a really great example for threading so um just wanted to cover those things uh the next ones that we'll be talking about uh we'll be back to more networking purposes sending and receiving data using you know encoding and decoding that data so it works properly so stay tuned for that if you guys have any questions or comments on this I'll feel free to leave them below as always thank you for watching thanks for all the support and subscriptions and until next time no more ports are open
Original Description
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: http://pythonprogramming.net/beginner-python-programming-tutorials/
Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M
http://seaofbtc.com
http://sentdex.com
http://hkinsley.com
https://twitter.com/sentdex
Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: Tool Use & Function Calling
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Dev.to AI
Why I built a simple AI provider wrapper (and you might too)
Dev.to · zhongqiyue
This ChatGPT Prompt Replaced 3 Hours of PowerPoint Work
Medium · AI
This ChatGPT Prompt Replaced 3 Hours of PowerPoint Work
Medium · ChatGPT
🎓
Tutor Explanation
DeepCamp AI