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
Today I fixed a really C**Py bug which have been bugged me all days and years, nights and midnights!

I use a scheduler to to get data from MongoDB and one the servers is outside of Iran and another in Iran. When I want to get data sometimes querying the db takes forever and it freezes the data gathering procedure. I had to restart (like windows) to reset the connection. I know it was stupid! :|

I found the below parameter that you can set on your pymongo.MongoClient:

socketTimeoutMS=10000

socketTimeoutMS: (integer or None) Controls how long (in milliseconds) the driver will wait for a response after sending an ordinary (non-monitoring) database operation before concluding that a network error has occurred. Defaults to `None` (no timeout).
When you don't set it it means no timeout! So I set it to 20000 Ms (20 Sec) in order to solve this nasty problem.

#mongodb #mongo #socketTimeoutMS #timeout #socket_timeout