Threaded Port Scanner using Sockets in Python Last Updated : 28 Aug, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Port scanning can be really slow yet, in most cases, is not process intensive. Thus, we can use threading to improve our speed. There can be thousands of possible ports. If it takes 5-15 seconds per port to scan, then we might have a long wait ahead of us without the use of threading. Threading Threading is a complex topic, but it can be broken down and conceptualized as a methodology where we can tell the computer to do another task if the processor is experiencing idle time. In the case of port scanning, we are spending a lot of time just waiting on the response from the server. While we are waiting, we can do something else. That is what threading is used for. Example: In this program, we can scan a number of ports in a certain range. Python3 import threading from queue import Queue import time import socket # a print_lock is used to prevent "double" # modification of shared variables this is # used so that while one thread is using a # variable others cannot access it Once it # is done, the thread releases the print_lock. # In order to use it, we want to specify a # print_lock per thing you wish to print_lock. print_lock = threading.Lock() # ip = socket.gethostbyname(target) target = 'localhost' def portscan(port): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: con = s.connect((target, port)) with print_lock: print('port is open', port) con.close() except: print('port is close', port) # The threader thread pulls a worker # from a queue and processes it def threader(): while True: # gets a worker from the queue worker = q.get() # Run the example job with the available # worker in queue (thread) portscan(worker) # completed with the job q.task_done() # Creating the queue and threader q = Queue() # number of threads are we going to allow for for x in range(4): t = threading.Thread(target=threader) # classifying as a daemon, so they it will # die when the main dies t.daemon = True # begins, must come after daemon definition t.start() start = time.time() # 10 jobs assigned. for worker in range(1, 10): q.put(worker) # wait till the thread terminates. q.join() Output: port is close 2 port is close port is close 4 port is closeport is close 1 53 port is close 6port is close 7 port is close 8 port is close 9 Comment More infoAdvertise with us Next Article Threaded Port Scanner using Sockets in Python A ashishguru9803 Follow Improve Article Tags : Python Python-threading Practice Tags : python Similar Reads Simple Port Scanner using Sockets in Python Prerequisites: Socket Programming in Python Before going to the programming, let us discuss about ports. In this article, we will check the virtual ports of a server or websites, or localhost. Every port has a unique number. There are 65,535 ports available in a host starting from 0. We can assign t 3 min read Python - Simple Port Scanner with Sockets Ports can be scanned to check which ports are engaged and which ports are open or free. In Python "Socket" module provides access to the BSD socket interface, which is available on all platforms. To scan the ports, the following steps can be implemented: 1] Recognize the host's IP address 2] Create 2 min read Port Scanner using Python Prerequisites: Socket Programming in Python This article is just to provide a sample code to generate a Port Scanner. This Port Scanner will work for both the Web Applications as well as remote Host. This tool has been created to provide the basic functionality of a Port Scanner. The general concept 2 min read Socket Programming with Multi-threading in Python Prerequisite : Socket Programming in Python, Multi-threading in PythonWe are given a scenario where we need to handle multiple client connections to a server simultaneously. This can be achieved using socket programming along with multi-threading. Socket programming allows two machines to communicat 3 min read File Transfer using TCP Socket in Python In this article, we implement a well-known Protocol in Computer Networks called File Transfer Protocol (FTP) using Python. We use the TCP Socket for this, i.e. a connection-oriented socket. FTP (File Transfer Protocol) is a network protocol for transmitting files between computers over Transmission 4 min read Like