Senthil added the comment:
Hi,
The patch attached required a complete rewrite. I am attaching the
modified patch, which will just substitute socket.gethostbyname with a
function gethost_addrinfo which internally uses getaddrinfo and takes
care of the IPv4 or IPv6 addresses translation.
jjlee, skip: let me know your comments on this.
One note we have to keep in mind is, testing on IPv6 address.
For eg. on my system /etc/hosts
10.98.1.6 goofy.goofy.com
#fe80::219:5bff:fefd:6270 localhost
127.0.0.1 localhost
test_urllib2 will PASS for the above.
But if I uncomment the IPv6 address, opening the local file fails. I am
not sure how local file access is done with IPv6 and should urllib2
(local file opening function) itself needs to be modified. Shall check
into that, with next version.
----------
nosy: +orsenthil
_____________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1675455>
_____________________________________
--- urllib2.py 2007-09-25 09:05:46.346939560 +0530
+++ urllib2-getaddrinfo.py 2007-09-25 09:04:59.019134480 +0530
@@ -1193,6 +1193,24 @@
return [part.strip() for part in res]
+def gethost_addrinfo(hostname):
+ if socket.has_ipv6:
+ try:
+ for res in socket.getaddrinfo(hostname, None, socket.AF_INET6,
+ socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME):
+ af, socktype, proto, canonname, sa = res
+ except socket.gaierror:
+ for res in socket.getaddrinfo(hostname, None, socket.AF_INET,
+ socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME):
+ af, socktype, proto, canonname, sa = res
+ else:
+ for res in socket.getaddrinfo(hostname, None, socket.AF_INET,
+ socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME):
+ af, socktype, proto, canonname, sa = res
+
+ return sa[0]
+
+
class FileHandler(BaseHandler):
# Use local file or FTP depending on form of URL
def file_open(self, req):
@@ -1208,10 +1226,10 @@
def get_names(self):
if FileHandler.names is None:
try:
- FileHandler.names = (socket.gethostbyname('localhost'),
- socket.gethostbyname(socket.gethostname()))
+ FileHandler.names = (gethost_addrinfo('localhost'),
+ gethost_addrinfo(socket.gethostname()))
except socket.gaierror:
- FileHandler.names = (socket.gethostbyname('localhost'),)
+ FileHandler.names = (gethost_addrinfo('localhost'),)
return FileHandler.names
# not entirely sure what the rules are here
@@ -1232,7 +1250,7 @@
if host:
host, port = splitport(host)
if not host or \
- (not port and socket.gethostbyname(host) in self.get_names()):
+ (not port and gethost_addrinfo(host) in self.get_names()):
return addinfourl(open(localfile, 'rb'),
headers, 'file:'+file)
except OSError, msg:
@@ -1264,7 +1282,7 @@
passwd = unquote(passwd or '')
try:
- host = socket.gethostbyname(host)
+ host = gethost_addrinfo(host)
except socket.error, msg:
raise URLError(msg)
path, attrs = splitattr(req.get_selector())
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com