IPv6 and Python

Giampaolo Rodola' gnewsg at gmail.com
Sat May 3 13:27:12 EDT 2008


On 3 Mag, 05:38, Roy Smith <r... at panix.com> wrote:

> In the application I work on, we've avoided this.  We just listen on two
> separate sockets (one for each address family).  We wrote a DualSocket
> class which manages the two underlying single-protocol sockets and makes
> them appear to be a single dual-protocol socket.  It was a lot of user code
> to write, compared with using the mapped address mechanism, but at least
> it's portable to every OS we've seen that support IPv6.
>
> You don't need multi-threading to handle multiple sockets.  In our
> implementation, for example, we use select() in a single thread to
> multiplex the two.

I would be very interested in taking a look at how you implemented
that part of code. Would it be possible?
For now I wrote this and it seems to work just fine.
Any advice about it?



class FTPServer(asyncore.dispatcher):

    def __init__(self, address, handler):
        """Initiate the FTP server opening listening on address.

         - (tuple) address: the ip:port pair on which to listen
            for ftp data connections.
         - (classobj) handler: the handler class to use.
        """
        asyncore.dispatcher.__init__(self)
        self.handler = handler
        host, port = address
        for res in socket.getaddrinfo(host, port, 0,
socket.SOCK_STREAM):
            af, socktype, proto, canonname, sa = res
            try:
                self.create_socket(af, socket.SOCK_STREAM)
            except socket.error, msg:
                if self.socket:
                    self.socket.close()
                self.socket = None
                continue
            break
        if not self.socket:
            raise socket.error, msg
        self.address_family = af

        if os.name not in ('nt', 'ce'):
            self.set_reuse_addr()
        self.bind(address)
        self.listen(5)

    def handle_accept(self):
        sock_obj, addr = self.accept()
        log("[]%s:%s Connected." %addr[0:2])
        handler = self.handler(sock_obj, self)


--- Giampaolo
http://code.google.com/p/pyftpdlib/



More information about the Python-list mailing list