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?
Asked
Active
Viewed 3,015 times
2
-
possible duplicate of [Force python mechanize/urllib2 to only use A requests?](http://stackoverflow.com/questions/2014534/force-python-mechanize-urllib2-to-only-use-a-requests) – Wooble Jun 27 '12 at 16:50
-
or maybe [how to force python httplib library to use only A requests](http://stackoverflow.com/questions/1540749/how-to-force-python-httplib-library-to-use-only-a-requests)? – glglgl Jun 27 '12 at 16:55
-
`urllib.urlopen()` or `urllib2.urlopen()`? – glglgl Jun 27 '12 at 16:56
-
Update / Solution: I tried the suggestion as logged here stackoverflow.com/a/6319043/420284 and it does work for me.., even though it is ugly hack but probably the simplest for me. – Divick Jun 27 '12 at 17:18
1 Answers
1
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()
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.

glglgl
- 89,107
- 13
- 149
- 217
-
1I tried the suggestion as logged here http://stackoverflow.com/a/6319043/420284 and it does work for me.., even though it is ugly hack but probably the simplest for me. – Divick Jun 27 '12 at 17:17