[Python-Dev] IPv6 and Windows

Fredrik Lundh fredrik@pythonware.com
Mon, 25 Jun 2001 14:12:23 +0200


martin wrote:

> getaddrinfo offers protocol-independent address lookup. It is
> necessary to use that API to support AF_INET and AF_INET6
> transparently in application code. itojun proposes to change a number
> of standard library modules. Please have a look at the actual patch
> for details; the typical change will look like this (for httplib)
>
> diff -u -r1.35 httplib.py
> --- Lib/httplib.py 2001/06/01 16:25:38 1.35
> +++ Lib/httplib.py 2001/06/24 04:41:48
> @@ -357,10 +357,22 @@
>
>      def connect(self):
>          """Connect to the host and port specified in __init__."""
> -        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> -        if self.debuglevel > 0:
> -            print "connect: (%s, %s)" % (self.host, self.port)
> -        self.sock.connect((self.host, self.port))
> + for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
> +     af, socktype, proto, canonname, sa = res
> +     try:
> + self.sock = socket.socket(af, socktype, proto)
> + if self.debuglevel > 0:
> +     print "connect: (%s, %s)" % (self.host, self.port)
> + self.sock.connect(sa)
> +     except socket.error, msg:
> + if self.debuglevel > 0:
> +     print 'connect fail:', (self.host, self.port)
> + self.sock.close()
> + self.sock = None
> + continue
> +     break
> + if not self.sock:
> +     raise socket.error, msg

instead of adding code like that to every single module, maybe
we should add a convenience function to the socket module?

(and make that function smart enough to work also if getaddrinfo
isn't supported by the native platform...)

</F>