# ``twisted`.`internet`.`protocol`.`Factory``

class documentation

`class Factory: (source)`

Known subclasses: `twisted.conch.ssh.factory.SSHFactory`, `twisted.conch.ssh.forwarding.SSHListenForwardingFactory`, `twisted.internet.protocol.ClientFactory`, `twisted.internet.protocol.ServerFactory`, `twisted.protocols.portforward.ProxyFactory`, `twisted.protocols.socks.SOCKSv4Factory`, `twisted.protocols.socks.SOCKSv4IncomingFactory`, `twisted.words.protocols.irc.DccSendFactory`, `twisted.web.client._HTTP11ClientFactory`

Implements interfaces: `twisted.internet.interfaces.ILoggingContext`, `twisted.internet.interfaces.IProtocolFactory`

View In Hierarchy

This is a factory which produces protocols.

By default, buildProtocol will create a protocol of the class given in self.protocol.

Class Method | `forProtocol` | Create a factory for the given protocol.
Method | `buildProtocol` | Create an instance of a subclass of Protocol.
Method | `doStart` | Make sure startFactory is called.
Method | `doStop` | Make sure stopFactory is called.
Method | `logPrefix` | Describe this factory for log messages.
Method | `startFactory` | This will be called before I begin listening on a Port or Connector.
Method | `stopFactory` | This will be called before I stop listening on all Ports/Connectors.
Class Variable | `noisy` | Undocumented
Class Variable | `protocol` | Undocumented
Instance Variable | `numPorts` | Undocumented

@classmethod
def forProtocol(cls, protocol, *args, **kwargs): (source)

Create a factory for the given protocol.

It sets the `protocol` attribute and returns the constructed factory instance.

Parameters
protocol | A `Protocol` subclass
*args | Positional arguments for the factory.
**kwargs | Keyword arguments for the factory.
Returns
A `Factory` instance wired up to `protocol`.

def buildProtocol(self, addr): (source)
from `twisted.internet.interfaces.IProtocolFactory`
overridden in `twisted.conch.client.direct.SSHClientFactory`, `twisted.conch.scripts.tkconch.SSHClientFactory`, `twisted.conch.ssh.factory.SSHFactory`, `twisted.conch.ssh.forwarding.SSHListenForwardingFactory`, `twisted.mail.protocols.POP3Factory`, `twisted.mail.relaymanager.SMTPManagedRelayerFactory`, `twisted.mail.smtp.SMTPFactory`, `twisted.mail.smtp.SMTPSenderFactory`, `twisted.names.client.DNSClientFactory`, `twisted.names.server.DNSServerFactory`, `twisted.protocols.dict.DictLookupFactory`, `twisted.protocols.ftp.DTPFactory`, `twisted.protocols.ftp.FTPDataPortFactory`, `twisted.protocols.loopback.LoopbackClientFactory`, `twisted.protocols.policies.LimitTotalConnectionsFactory`, `twisted.protocols.policies.WrappingFactory`, `twisted.protocols.portforward.ProxyClientFactory`, `twisted.protocols.socks.SOCKSv4Factory`, `twisted.protocols.socks.SOCKSv4IncomingFactory`, `twisted.spread.pb.PBClientFactory`, `twisted.spread.pb.PBServerFactory`, `twisted.web.http.HTTPFactory`, `twisted.web.proxy.ProxyClientFactory`, `twisted.words.protocols.irc.DccChatFactory`, `twisted.words.protocols.irc.DccSendFactory`, `twisted.words.protocols.jabber.xmlstream.XmlStreamServerFactory`, `twisted.words.xish.xmlstream.XmlStreamFactory`, `twisted.internet.endpoints._WrappingFactory`, `twisted.internet.protocol._InstanceFactory`, `twisted.protocols.ftp._PassiveConnectionFactory`, `twisted.web.client._HTTP11ClientFactory`

Create an instance of a subclass of Protocol.

The returned instance will handle input on an incoming server connection, and an attribute "factory" pointing to the creating factory.

Alternatively, `None` may be returned to immediately close the new connection.

Override this method to alter how Protocol instances get created.

Parameters
addr:`IAddress` | an object implementing `IAddress`
Returns
`Optional[Protocol]` | Undocumented

def doStart(self): (source)
from `twisted.internet.interfaces.IProtocolFactory`
overridden in `twisted.protocols.policies.WrappingFactory`, `twisted.internet.endpoints._WrappingFactory`

Make sure startFactory is called.

Users should not call this function themselves!

def doStop(self): (source)
from `twisted.internet.interfaces.IProtocolFactory`
overridden in `twisted.protocols.policies.WrappingFactory`, `twisted.internet.endpoints._WrappingFactory`

Make sure stopFactory is called.

Users should not call this function themselves!

def logPrefix(self): (source)
from `twisted.internet.interfaces.ILoggingContext`
overridden in `twisted.protocols.policies.WrappingFactory`
Describe this factory for log messages.
def startFactory(self): (source)
overridden in `twisted.conch.ssh.factory.SSHFactory`, `twisted.protocols.policies.LimitConnectionsByPeer`, `twisted.web.http.HTTPFactory`

This will be called before I begin listening on a Port or Connector.

It will only be called once, even if the factory is connected to multiple ports.

This can be used to perform 'unserialization' tasks that are best put off until things are actually running, such as connecting to a database, opening files, etcetera.

def stopFactory(self): (source)
overridden in `twisted.conch.scripts.tkconch.SSHClientFactory`, `twisted.protocols.ftp.DTPFactory`, `twisted.protocols.ftp.FTPFactory`, `twisted.web.http.HTTPFactory`

This will be called before I stop listening on all Ports/Connectors.

This can be overridden to perform 'shutdown' tasks such as disconnecting database connections, closing files, etc.

It will be called, for example, before an application shuts down, if it was connected to a port. User code should not call this function directly.

noisy: `bool` = (source)
overridden in `twisted.conch.scripts.tkconch.SSHClientFactory`, `twisted.protocols.ftp.FTPDataPortFactory`, `twisted.protocols.tls.TLSMemoryBIOFactory`, `twisted.words.protocols.irc.DccChatFactory`, `twisted.internet.protocol._InstanceFactory`, `twisted.protocols.ftp._PassiveConnectionFactory`

Undocumented

protocol: `Optional[Callable[[], Protocol]]` = (source)
overridden in `twisted.mail.protocols.POP3Factory`, `twisted.mail.protocols.SMTPFactory`, `twisted.mail.relaymanager.SMTPManagedRelayerFactory`, `twisted.names.server.DNSServerFactory`, `twisted.protocols.loopback.LoopbackClientFactory`

Undocumented

numPorts = (source)

Undocumented