• Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
AnonyViet - English Version
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
AnonyViet - English Version
No Result
View All Result

Python Network Security – Lesson 1: Port Scanner

AnonyViet by AnonyViet
January 28, 2023
in Tips
0

In this article, I will show you how to create the simplest Port Scanner in Python. The program will use 2 very popular libraries: socket and sys. If you don’t know what python is, you can read it Python knowledge lesson for newbies Learn the basics of python quickly. In this article, we will write a program to scan network ports to see which ports are open using python

Join the channel Telegram of the AnonyViet 👉 Link 👈

port scanner python

Sockets Library

This is one of the standard libraries used for low level network interaction. The socket() function returns a socket object with various socket system calls. Parameter types are somewhat more flexible in the C interface.

Basic Functions

Some of the basic functions that we will use throughout this article will have the following functions:

  • socket.gethostbyname: Gets the website domain name from the user and returns the host’s IP. Eg:
>>> import socket
>>> socket.gethostbyname("www.google.com")
'216.58.199.132'

OOP function

socket.socket (AF_INET, SOCK_STREAM): This is an OOP function class, which means you need to provide objects (data) for the class to process.

Eg:

>>> from sockets import *
>>>s = socket(AF_INET, SOCK_STREAM)
  • s.connect (host, port):s is a variable used to call classes in the socket library. This command will connect to the specified server’s port. Eg:
>>> from socket import *
>>> s = socket(AF_INET, SOCK_STREAM)
>>> s.connect(('216.58.199.132',80))
  • s.recv: This command will receive data from the server.

Create Port Scanner in Python

Apply the above knowledge to create port scanner:

from socket import *

def port_scan(host, port):
    s = socket(AF_INET, SOCK_STREAM) # Thiết lập giao thức TCP
    try: # Exception Handling
        s.connect((host, port)) # Kết nối với port
        print("[+] {} port is open".format(port))
    except: # If connection fails
        print("[+] Port is closed")

How it works

First the program will import all the functions/classes from the library socket. def port_scan function definition port_scan and it has two parameters host and portthen the program will set up the TCP protocol and try….except will handle error/exception cases. s.connect will try to connect to the server port.

Optimize the code for the 1st time

Save the file as port_scan.py

from socket import *

def port_scan(host, port):
    s = socket(AF_INET, SOCK_STREAM) # Thiết lập giao thức TCP
    try: # Exception Handling
        s.connect((host, port)) # Kết nối với port
        print("[+] {} port is open".format(port))
    except: # If connection fails
        print("[+] Error Occured")
        
def main():
   host = input("Enter Host: ")
   port = input("Enter Port: ")
   port_scan(host, port) # Gọi hàm port_scan
   
if __name__ == '__main__':
    main()

We added the main function to ask the user for input host and port then will call the function port_scan. Jaw port_scan will execute the commands contained in the function.

Optimize the code for the 2nd time

Now, I will show you how to add parameters in terminal and scan various ports.

from socket import *
import sys

def port_scan(host): 
    for i in range(1, 1025):
        s = socket(AF_INET, SOCK_STREAM)# Thiết lập giao thức TCP
        res = s.connect_ex((str(host), i))
        if res ==  0: # Nếu kết nối thành công
            print("Port {} is open.".format((i)))
        s.close() # Đóng kết nối

   
if __name__ == '__main__':
    port_scan(sys.argv[1])

How it works

Same as above but it has command s.connec_ex new, like s.connect but it gives the result as a number, for example 1 means an error has occurred and 0 means success. The first two lines are import sockets and sys. Then we will scan from port number 1 to port 1024. After scanning 1 port, we have to close the connection because it will create a socket associated with the server.

Note: I import sys to read the parameters on the terminal with sys.argv [1] because 1 is the location of the server.

How to use file port_scan.py

Now, run the port_scan file and remember to add the host ip parameter. The host ip is the ip of the machine you are using.

robin@oracle:/Projects$ python3 port_scan.py 192.168.43.172
Port 22 is open.

So you have successfully created a port scanner using python. Alternatively, you can also create a Keylogger using python here.

The article achieved: 5/5 – (100 votes)

Tags: LessonnetworkPortPythonScannersecurity
Previous Post

Convert Physical Computer to Virtual Machine – Windows Server 2012 R2

Next Post

Share Tool Hades used to Troll and Phishing Password Windows

AnonyViet

AnonyViet

Related Posts

Instructions on how to format text on the Windows 11 notepad
Tips

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
4 ways to fix bluetooth connectivity on Windows 11
Tips

4 ways to fix bluetooth connectivity on Windows 11

August 8, 2025
How to know the computer is tracked and processed by Keylogger
Tips

How to know the computer is tracked and processed by Keylogger

August 7, 2025
Opal: Create applications who do not need to write code
Tips

Opal: Create applications who do not need to write code

August 3, 2025
How to activate a new Start menu on Windows 11
Tips

How to activate a new Start menu on Windows 11

July 29, 2025
Intellgpt: AI tool for osint and data science
Tips

Intellgpt: AI tool for osint and data science

July 28, 2025
Next Post
Share Tool Hades used to Troll and Phishing Password Windows

Share Tool Hades used to Troll and Phishing Password Windows

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

Instructions on how to format text on the Windows 11 notepad

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
Instructions for receiving 80GB of free data from VinaPhone from August 15

Instructions for receiving 80GB of free data from VinaPhone from August 15

August 15, 2025
Online driving exam preparation: Support theory and practice

Online driving exam preparation: Support theory and practice

August 15, 2025
How to add application to your favorite bar

How to add application to your favorite bar

August 14, 2025
Instructions on how to format text on the Windows 11 notepad

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
Instructions for receiving 80GB of free data from VinaPhone from August 15

Instructions for receiving 80GB of free data from VinaPhone from August 15

August 15, 2025
Online driving exam preparation: Support theory and practice

Online driving exam preparation: Support theory and practice

August 15, 2025
AnonyViet - English Version

AnonyViet

AnonyViet is a website share knowledge that you have never learned in school!

We are ready to welcome your comments, as well as your articles sent to AnonyViet.

Follow Us

Contact:

Email: anonyviet.com[@]gmail.com

Main Website: https://anonyviet.com

Recent News

Instructions on how to format text on the Windows 11 notepad

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
Instructions for receiving 80GB of free data from VinaPhone from August 15

Instructions for receiving 80GB of free data from VinaPhone from August 15

August 15, 2025
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí mm88 8XBET mm88 trang chủ new88

No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí mm88 8XBET mm88 trang chủ new88

wpDiscuz
0
0
Would love your thoughts, please comment.x
()
x
| Reply