默认情况下如何通过ipv4进行urlopen [英] how to do urlopen over ipv4 by default
问题描述
在python中执行urlopen的方法是什么,即使底层计算机启用了ipv6网络,请求也是通过ipv4而不是ipv6发送的?
What is the way to do urlopen in python such that even if the underlying machine has ipv6 networking enabled, the request is sent via ipv4 instead of ipv6?
推荐答案
我查看了源代码.不幸的是,urllib.urlopen()
似乎使用了httplib.HTTP()
,甚至不允许设置源地址.
I had a look into the source code. Unfortunately, urllib.urlopen()
seems to use httplib.HTTP()
, which doesn't even allow setting a source address.
urllib2.urlopen()
使用您可以从中继承的httplib.HTTPConnection()
并创建一个默认情况下设置源地址'0.0.0.0'
而不是''
的类.然后,您可以通过创建新" HTTPHandler()
(在urllib2.py
中查看它的工作方式)和一个新的打开器(build_opener()
和/或install_opener()
)将新的重写类注入到urllib2
内容中.
urllib2.urlopen()
uses httplib.HTTPConnection()
which you could inherit from and create a class which by default sets a source address '0.0.0.0'
instead of ''
. Then you could somehow inject that new overridden class into the urllib2
stuff by creating a "new" HTTPHandler()
(look how it's done in urllib2.py
) and a new opener which you build_opener()
and/or install_opener()
.
对不起,我不太准确,但是我从来没有做过这样的事情,也不知道它是如何工作的.
Sorry for not being very exact, but I never have done such a thing and don't know exactly how that works.
这篇关于默认情况下如何通过ipv4进行urlopen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!