• 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 👈

Python Network Security – Lesson 1: Port Scanner

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 edit Facebook scheduled posts
Tips

Instructions on how to edit Facebook scheduled posts

November 16, 2025
Suggested AI command to create a rustic and charming image of a Ban Lien girl
Tips

Suggested AI command to create a rustic and charming image of a Ban Lien girl

November 16, 2025
Windows 10 will have a new, simpler interface
Tips

Windows 10 will have a new, simpler interface

November 16, 2025
How to check what data Facebook is controlling about you
Tips

How to check what data Facebook is controlling about you

November 15, 2025
How to record Chromebook screen – ChromeOS
Tips

How to record Chromebook screen – ChromeOS

November 14, 2025
How to create photos under the snow extremely quickly in just a few minutes
Tips

How to create photos under the snow extremely quickly in just a few minutes

November 13, 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 edit Facebook scheduled posts

Instructions on how to edit Facebook scheduled posts

November 16, 2025
Suggested AI command to create a rustic and charming image of a Ban Lien girl

Suggested AI command to create a rustic and charming image of a Ban Lien girl

November 16, 2025
Windows 10 will have a new, simpler interface

Windows 10 will have a new, simpler interface

November 16, 2025
Share Key Active Visual Studio 2026

Share Key Active Visual Studio 2026

November 15, 2025
Instructions on how to edit Facebook scheduled posts

Instructions on how to edit Facebook scheduled posts

November 16, 2025
Suggested AI command to create a rustic and charming image of a Ban Lien girl

Suggested AI command to create a rustic and charming image of a Ban Lien girl

November 16, 2025
Windows 10 will have a new, simpler interface

Windows 10 will have a new, simpler interface

November 16, 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 edit Facebook scheduled posts

Instructions on how to edit Facebook scheduled posts

November 16, 2025
Suggested AI command to create a rustic and charming image of a Ban Lien girl

Suggested AI command to create a rustic and charming image of a Ban Lien girl

November 16, 2025
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

new88 trang chủ shbet trang chủ f168 trang chủ qq88 nhà cái 78win

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

new88 trang chủ shbet trang chủ f168 trang chủ qq88 nhà cái 78win

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