N
N
NeoLight22020-08-01 12:58:06
Python
NeoLight2, 2020-08-01 12:58:06

Is there a timeout on a Python socket?

There is a function that uses the Socket class to send and receive an array of bytes.

def CheckServer(IP,Port):
    
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        
        for i in range (5):
            sock.sendto(smertlogin, (IP, Port))
        data = sock.recv(1024)
        
        ndata = str(data)
        if "fe" in ndata and "fd" in ndata:
            print(color_green + "[+] " + IP + ":" + str(Port) + " Is 2.2.5 SERVER !" + reset)
        else:
            print(color_r + "[-] " + IP + ":" + str(Port) + " this is not 2.2.5 Server" + reset)

    except:
        print(color_r + "[-] " + IP + ":" + str(Port) + " this is not 2.2.5 Server" + reset)


The question is, is it possible to use the sock.recv(1024) function with a timeout for a fixed time ?
Well, for example, the sock.recv(1024) function will listen for exactly 10 seconds. after which the function will end?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Miit, 2020-08-01
@NeoLight2

There is already an answer here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question