我将如何在Python脚本中增加DNS分辨率超时?

发布于 2025-02-04 15:19:37 字数 1481 浏览 2 评论 0原文

运行我的Python代码的服务器,从网站上获取一些数据必须跳过这么多的箍,以便到达DNS服务器,以至于延迟达到了DNS分辨率的时间(这已经不在我的控件中)。有时它有效,有时行不通。

因此,我正在尝试进行一些例外处理,并试图确保其有效。

目标:

  1. 增加DNS超时。我不确定一个好时光,但让我们30秒钟。
  2. 尝试解决该网站5次,如果它解决了,请继续刮擦网站。如果没有,请继续尝试,直到5次尝试开始。

这是以Google.com为例的代码。

import socket
import http.client

#Confirm that DNS resolution is successful.
def dns_lookup(host):
    try:
        socket.getaddrinfo(host, 80)
    except socket.gaierror:
        return "DNS resolution to the host failed."
    return True

#Scrape the targeted website.
def request_website_data():
    conn = http.client.HTTPConnection("google.com")
    conn.request("GET", "/")
    res = conn.getresponse()
    if (res.status == 200):
        print("Connection to the website worked! Do some more stuff...")
    else:
        print("Connection to the website did not work. Terminating.")

#Attempt DNS resolution 5 times, if it succeeds then immediately request the website and break the loop.
for x in range(5):
    dns_resolution = dns_lookup('google.com')

    if dns_resolution == True:
        request_website_data()
        break
    else:
        print(dns_resolution)

我正在浏览插座库 socket.socket.settime.settime.settime.settime.time out(value)<)< /a>,我不确定这是否是我想要的。我会插入什么代码中,以便有更宽容和更长的DNS解决时间?

谢谢。

The server that runs my Python code which get some data off of a website has to jump through so many hoops just to get to the DNS server that the latency gets to the point where it times out the DNS resolution (this is out of my control). Sometimes it works, sometimes it doesn't.

So I am trying to do some exception handling and try to ensure that it works.

Goal:

  1. Increase the DNS timeout. I am unsure of a good time but let's go with 30 seconds.
  2. Try to resolve the website 5 times, if it resolves, proceed to scrape the website. If it doesn't, keep trying until the 5 attempts are up.

Here is the code using google.com as an example.

import socket
import http.client

#Confirm that DNS resolution is successful.
def dns_lookup(host):
    try:
        socket.getaddrinfo(host, 80)
    except socket.gaierror:
        return "DNS resolution to the host failed."
    return True

#Scrape the targeted website.
def request_website_data():
    conn = http.client.HTTPConnection("google.com")
    conn.request("GET", "/")
    res = conn.getresponse()
    if (res.status == 200):
        print("Connection to the website worked! Do some more stuff...")
    else:
        print("Connection to the website did not work. Terminating.")

#Attempt DNS resolution 5 times, if it succeeds then immediately request the website and break the loop.
for x in range(5):
    dns_resolution = dns_lookup('google.com')

    if dns_resolution == True:
        request_website_data()
        break
    else:
        print(dns_resolution)

I am looking through the socket library socket.settimeout(value) and I am unsure if that's what I'm looking for. What would I insert into my code to have a more forgiving and longer DNS resolution time?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文