D
D
Denis2016-02-01 12:49:13
Python
Denis, 2016-02-01 12:49:13

DNS query timeout in Python: how to reduce?

Good afternoon.
There is a task to look into a large number of links with a script. Everything would be fine, but some domains cannot be resolved to the address. In this case, the script waits for a long, long time for a server response.
How can I make it so that he waits much less?
A cursory googling didn't come up with a solution.
Everything is sawn on the third Python, urllib3 is used, I resolve the domain using socket.gethostbyname.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
lega, 2016-02-01
@Ayahuaska

There is a task to look into a large number of links with a script.
Use async frameworks for this.
Here is an example on gevent, in parallel (a bunch of coroutines) with a timeout www.gevent.org/intro.html#example

N
nirvimel, 2016-02-01
@nirvimel

Indeed, gethostbyname (as well as getaddrinfo, through which it is implemented) does not provide any way to set a timeout. All that remains is to parallelize work through threading or through gevent (the documentation has a good example of parallel name resolution).

S
sim3x, 2016-02-01
@sim3x

https://docs.python.org/3/library/socket.html#sock... set
a caching dns timeout on your host set it
to 5-10 seconds
parallel parser is a must anyway

D
Denis, 2016-02-01
@Ayahuaska

In general, I summarize: the comrade lega
stated what I myself thought about and, moreover, gave a sensible link. nirvimel gave the same useful link and confirmed the fact that settimeout has an effect (Spoiler: it has NO effect). sim3x suggested the idea with his DNS server. I'll keep it in reserve.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question