[Twisted-Python] fdport

Moshe Zadka moshez at twistedmatrix.com
Tue Aug 20 02:23:55 EDT 2002


import socket
from twisted.internet import tcp, abstract
from twisted.python import log

class FDPort(tcp.Port):

    port = "dummy"

    def __init__(self, fd, factory, backlog=None, reactor=None):
        """Initialize with a numeric port to listen on.
        """
        abstract.FileDescriptor.__init__(self, reactor=reactor)
        self.fd = fd
        self.factory = factory
        if backlog:
	    self.backlog = backlog

    def __getstate__(self):
        """(internal) get my state for persistence
        """
        dct = copy.copy(self.__dict__)
        try: del dct['socket']
        except: pass
        return dct

    def startListening(self):
        """Create and bind my socket, and begin listening on it.

        This is called on unserialization, and must be called after creating a
        server to begin listening on the specified port.
        """
        log.msg("%s startings"% self.factory.__class__)
        self.factory.doStart()
        skt = socket.fromfd(self.fd, socket.AF_INET,socket.SOCK_STREAM)
        skt.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
        skt.setblocking(0)
        skt.listen(self.backlog)
        self.connected = 1
        self.socket = skt
        self.numberAccepts = 100
        self.fileno = skt.fileno
        self.startReading()




More information about the Twisted-Python mailing list