让 Python 在下载的时候使用 IPv4 域名解析 - Jacky Liu's Blog

让 Python 在下载的时候使用 IPv4 域名解析

Jacky Liu posted @ 2011年7月16日 03:30 in Python with tags python DNS IPv4 地址解析 域名解析 , 3711 阅读

---- 这个话题由来于我想从深交所的网站扒些信息下来,发现用 Python 下载时,每次都要 30 秒才能连上,浏览器访问却正常。于是查找原因,最后用抓包分析,发现问题出在域名解析阶段。浏览器访问时,向 DNS 服务器发送的是 A 型查询,要求做 IPv4 地址解析; 而 Python 的 urllib.request 模块发送的却是 AAAA 型查询,要求做 IPv6 地址解析,对深交所网址进行 IPv6 解析居然要花 30 秒,不知道为什么。

---- 然后就是各种 Google + 查看代码,追根溯源。不细说了,只贴结果:

 

def Resolver_IPv4(hostname):
	return socket.gethostbyname(hostname)    # 此函数返回 IPv4 地址
													
class HTTPConnection_IPv4(http.client.HTTPConnection):
	def connect(self):
		self.sock= socket.create_connection((Resolver_IPv4(self.host), self.port), self.timeout)

class HTTPHandler_IPv4(urllib.request.HTTPHandler):
	def http_open(self, req):
		return self.do_open(HTTPConnection_IPv4, req)
													
urlopen_IPv4= urllib.request.build_opener(HTTPHandler_IPv4).open

 

如此以后,用这个 urlopen_IPv4() 函数来取代平常用的 urllib.request.urlopen() 函数,就可以用 IPv4 地址解析的方式来下载:

 

webfile= urlopen_IPv4(fullurl='http://www.szse.cn/', timeout=3)


---- 最后说一下,抓包感觉就跟作弊一样,爽!

Avatar_small
tank trouble 说:
2018年10月25日 10:05

于是查找原因,最后用抓包分析,发现问题出在域名解析阶段。浏览器访问时...

Avatar_small
Samagra Question Poo 说:
2022年9月07日 21:04

Here we give the Samagra question pool questions in PDF format. This help students with their reference. We provide the PDF of all chapters in a single file. Social Science, Biology, Chemistry, Maths questions in all chapters are available. Samagra Question Pool This PDF note is also helpful for the Kerala PSC exam. The new questions from Kerala PSC are from Samagra questions. Class 10 Samagara Question Pool is below. You can easily download it as a PDF file. The Samagra portal aims to give the study materials in digital form. Below we give the Samagra question pool. You can easily download or read the notes from here.

Avatar_small
celeb networth 说:
2023年4月12日 16:34

Thank you so much for the wisdom. If you want to know more about the net worth of any celebrity, celeb networth post would be a good choice.

Avatar_small
प्रश्नपत्र.com 说:
2023年5月18日 11:57

High School Examination for CGBSE 6th, 7th, 8th, and 9th Model Paper 2023 Will Be Conducted by the Chhattisgarh Board of Secondary Education (CGBSE) Overview of the Chhattisgarh Board 5th, 6th, 7th, and 8th Exam Schedule Details Board's name is the Chhattisgarh प्रश्नपत्र.com School Examination Board, or CGBSE. Fifth Chhattisgarh Board Exam Name Overview of the Chhattisgarh Board's 5th, 6th, 7th, 8th, and 9th Exam Schedule Details Name of the Board: Chhattisgarh Board of Secondary Education, also known as CGBSE, will conduct high school examinations for the CGBSE 6th, 7th, 8th, and 9th Model Paper 2023.

Avatar_small
anonymous 说:
2023年8月06日 15:28

Excellent read, Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Kissimmee Wildlife Removal

Avatar_small
anonymous 说:
2023年8月12日 15:27

I read this article. I think You put a lot of effort to create this article. I appreciate your work. Orlando Wildlife Removal

Avatar_small
anonymous 说:
2023年8月12日 15:30

This is exciting, nevertheless it is vital for you to visit this specific url: Wildlife Removal Oviedo


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee