Python 3 Programming Tutorial - Threaded port scanner

sentdex · Beginner ·🛠️ AI Tools & Apps ·11y ago

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 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This tutorial teaches how to create a threaded port scanner in Python 3, improving performance by running multiple threads concurrently. It covers defining a port scanner function, creating threads, and managing a queue to assign ports to be scanned. By following this tutorial, you can learn how to use the threading module to speed up port scanning.

Key Takeaways
  1. Define a port scanner function
  2. Create a threader function
  3. Create 30 threads using the threading module
  4. Run each thread with the port scanner function and a worker from the queue
  5. Assign 100 ports to be scanned with 100 threads
  6. Put workers to work with q.put
  7. Join threads with q.join
💡 Using the threading module can significantly improve the performance of a port scanner by allowing multiple ports to be scanned concurrently.

Related AI Lessons

Up next
Salesforce Flow New Features (Summer '26) | Open Record, URL & Show Toast Messages
AITECHONE
Watch →