Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
How to create a network scanner in python using socket?

socket library is used to work with network and here we use it to simulate a network scanner.

A network scanner iterates over all the possible IP addresses on the network and for every IP it tries to connect to one of the ports. If the connection was successful (ACK received) we know that this host is available at the given IP address. This is the philosophy of a network scanner.

For now we just try to connect to a specific port of the server and leave the other parts to you to iterate and check the status:

import socket

addr = '172.16.132.20'
port = 27017

socket_obj = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = socket_obj.connect_ex((addr,port))
print result
socket_obj.close()

The above code connects to the given address:port and returns a status code. If returned code is 0 then the
port is open otherwise the port is probably blocked/closed.

#python #socket #network #tcp #tcp_scanner