Go to the latest version of this document.

# ``twisted`.`internet`.`udp`.`Port``

class documentation

`class Port(base.BasePort): (source)`

Known subclasses: `twisted.internet.udp.MulticastPort`, `twisted.internet.unix.DatagramPort`

Constructor: `Port(port, proto, interface, maxPacketSize, reactor)`

Implements interfaces: `twisted.internet.interfaces.IListeningPort`, `twisted.internet.interfaces.ISystemHandle`, `twisted.internet.interfaces.IUDPTransport`

View In Hierarchy

UDP port, listening for packets.

Method | `__init__` | No summary
Method | `__repr__` | Undocumented
Method | `connect` | 'Connect' to remote server.
Method | `connectionLost` | Cleans up my socket.
Method | `doRead` | Called when my socket is ready for reading.
Method | `getBroadcastAllowed` | Checks if broadcast is currently allowed on this port.
Method | `getHandle` | Return a socket object.
Method | `getHost` | Return the local address of the UDP connection
Method | `logPrefix` | Return the prefix to log with.
Method | `loseConnection` | Close the connection at the next available opportunity.
Method | `setBroadcastAllowed` | Set whether this port may broadcast. This is disabled by default.
Method | `setLogStr` | Initialize the `logstr` attribute to be used by `logPrefix`.
Method | `startListening` | Create and bind my socket, and begin listening on it.
Method | `stopListening` | Stop listening on this port.
Method | `write` | Write a datagram.
Method | `writeSequence` | Write a datagram constructed from an iterable of `bytes`.
Instance Variable | `addressFamily` | `socket.AF_INET` or `socket.AF_INET6`, depending on whether this port is listening on an IPv4 address or an IPv6 address.
Instance Variable | `connected` | Undocumented
Instance Variable | `d` | Undocumented
Instance Variable | `interface` | Undocumented
Instance Variable | `logstr` | Undocumented
Instance Variable | `maxPacketSize` | Undocumented
Instance Variable | `maxThroughput` | Maximum number of bytes read in one event loop iteration.
Instance Variable | `port` | Undocumented
Instance Variable | `protocol` | Undocumented
Instance Variable | `socket` | Undocumented
Class Method | `_fromListeningDescriptor` | Create a new `Port` based on an existing listening SOCK_DGRAM socket.
Method | `_bindSocket` | Prepare and assign a `socket.socket` instance to `self.socket`.
Method | `_connectToProtocol` | Undocumented
Method | `_loseConnection` | Undocumented
Method | `_setAddressFamily` | Resolve address family for the socket.
Instance Variable | `_connectedAddr` | Undocumented
Instance Variable | `_preexistingSocket` | If not `None`, a `socket.socket` instance which was created and initialized outside of the reactor and will be used to listen for connections (instead of a new socket being created by this `Port`).
Instance Variable | `_realPortNumber` | Actual port number being listened on. The value will be `None` until this `Port` is listening.

Inherited from `BasePort`:

Method | `createInternetSocket` | Undocumented
Method | `doWrite` | Raises a RuntimeError
Class Variable | `socketType` | Undocumented

Inherited from `FileDescriptor` (via `BasePort`):

Method | `fileno` | File Descriptor number for select().
Method | `getPeer` | Get the remote address of this connection.
Method | `loseWriteConnection` | Undocumented
Method | `pauseProducing` | Pause producing data.
Method | `readConnectionLost` | Indicates read connection was lost.
Method | `resumeProducing` | Resume producing data.
Method | `startReading` | Start waiting for read availability.
Method | `startWriting` | Start waiting for write availability.
Method | `stopConsuming` | Stop consuming data.
Method | `stopProducing` | Stop producing data.
Method | `stopReading` | Stop waiting for read availability.
Method | `stopWriting` | Stop waiting for write availability.
Method | `writeConnectionLost` | Indicates write connection was lost.
Method | `writeSomeData` | Write as much as possible of the given data, immediately.
Constant | `SEND_LIMIT` | Undocumented
Class Variable | `bufferSize` | Undocumented
Class Variable | `disconnecting` | Undocumented
Instance Variable | `dataBuffer` | Undocumented
Instance Variable | `disconnected` | Undocumented
Instance Variable | `offset` | Undocumented
Instance Variable | `producer` | Undocumented
Instance Variable | `producerPaused` | Undocumented
Instance Variable | `reactor` | Undocumented
Method | `_closeWriteConnection` | Undocumented
Method | `_isSendBufferFull` | Determine whether the user-space send buffer for this transport is full or not.
Method | `_maybePauseProducer` | Possibly pause a producer, if there is one and the send buffer is full.
Method | `_postLoseConnection` | Called after a loseConnection(), when all data has been written.
Class Variable | `_writeDisconnected` | Undocumented
Instance Variable | `_tempDataBuffer` | Undocumented
Instance Variable | `_tempDataLen` | Undocumented
Instance Variable | `_writeDisconnecting` | Undocumented

Inherited from `_ConsumerMixin` (via `BasePort`, `FileDescriptor`):

Method | `registerProducer` | Register to receive data from a producer.
Method | `unregisterProducer` | Stop consuming data from a producer, without disconnecting.
Instance Variable | `streamingProducer` | `bool` or `int`

Inherited from `_LogOwner` (via `BasePort`, `FileDescriptor`, `_ConsumerMixin`):

Method | `_getLogPrefix` | Determine the log prefix to use for messages related to `applicationObject`, which may or may not be an `interfaces.ILoggingContext` provider.

def __init__(self, port, proto, interface='', maxPacketSize=8192, reactor=None): (source)
overrides `twisted.internet.abstract.FileDescriptor.__init__`
overridden in `twisted.internet.udp.MulticastPort`, `twisted.internet.unix.DatagramPort`

Parameters
port:`int` | A port number on which to listen.
proto:`twisted.internet.protocol.DatagramProtocol` | A `DatagramProtocol` instance which will be connected to the given `port`.
interface:`str` | The local IPv4 or IPv6 address to which to bind; defaults to '', ie all IPv4 addresses.
maxPacketSize:`int` | The maximum packet size to accept.
reactor:`interfaces.IReactorFDSet` | A reactor which will notify this `Port` when its socket is ready for reading or writing. Defaults to `None`, ie the default global reactor.

def __repr__(self) -> `str`: (source)
overridden in `twisted.internet.unix.DatagramPort`

Undocumented

def connect(self, host, port): (source)
from `twisted.internet.interfaces.IUDPTransport`

'Connect' to remote server.

def connectionLost(self, reason=None): (source)
from `twisted.internet.interfaces.IFileDescriptor`
overrides `twisted.internet.abstract.FileDescriptor.connectionLost`
overridden in `twisted.internet.unix.DatagramPort`

Cleans up my socket.

def doRead(self): (source)
from `twisted.internet.interfaces.IReadDescriptor`
overrides `twisted.internet.abstract.FileDescriptor.doRead`
overridden in `twisted.internet.unix.ConnectedDatagramPort`

Called when my socket is ready for reading.

def getBroadcastAllowed(self): (source)
from `twisted.internet.interfaces.IUDPTransport`

Checks if broadcast is currently allowed on this port.

Returns
`bool` | Whether this port may broadcast.

def getHandle(self): (source)
from `twisted.internet.interfaces.ISystemHandle`

Return a socket object.

def getHost(self): (source)
from `twisted.internet.interfaces.IListeningPort`
overrides `twisted.internet.abstract.FileDescriptor.getHost`

Return the local address of the UDP connection

Returns
`IPv4Address` or `IPv6Address` | the local address of the UDP connection

def logPrefix(self): (source)
from `twisted.internet.interfaces.ILoggingContext`
overrides `twisted.internet.abstract._LogOwner.logPrefix`

Return the prefix to log with.

def loseConnection(self): (source)
from `twisted.internet.interfaces.ITransport`
overrides `twisted.internet.abstract.FileDescriptor.loseConnection`

Close the connection at the next available opportunity.

Call this to cause this FileDescriptor to lose its connection. It will first write any data that it has buffered.

If there is data buffered yet to be written, this method will cause the transport to lose its connection as soon as it's done flushing its write buffer. If you have a producer registered, the connection won't be closed until the producer is finished. Therefore, make sure you unregister your producer when it's finished, or the connection will never close.

def setBroadcastAllowed(self, enabled): (source)
from `twisted.internet.interfaces.IUDPTransport`

Set whether this port may broadcast. This is disabled by default.

Parameters
enabled:`bool` | Whether the port may broadcast.

def setLogStr(self): (source)
overridden in `twisted.internet.unix.DatagramPort`

Initialize the `logstr` attribute to be used by `logPrefix`.

def startListening(self): (source)
from `twisted.internet.interfaces.IListeningPort`
overridden in `twisted.internet.unix.ConnectedDatagramPort`

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.

def stopListening(self): (source)
from `twisted.internet.interfaces.IListeningPort`

Stop listening on this port.

If it does not complete immediately, will return Deferred that fires upon completion.

def write(self, datagram, addr=None): (source)
from `twisted.internet.interfaces.IUDPTransport`
overrides `twisted.internet.abstract.FileDescriptor.write`
overridden in `twisted.internet.unix.DatagramPort`

Write a datagram.

Parameters
datagram:`bytes` | The datagram to be sent.
addr:`tuple` containing `str` as first element and `int` as second element, or `None` | A tuple of (stringified IPv4 or IPv6 address, integer port number); can be `None` in connected mode.

def writeSequence(self, seq, addr): (source)
from `twisted.internet.interfaces.ITransport`
overrides `twisted.internet.abstract.FileDescriptor.writeSequence`

Write a datagram constructed from an iterable of `bytes`.

Parameters
seq:an iterable of `bytes` | The data that will make up the complete datagram to be written.
addr:`tuple` containing `str` as first element and `int` as second element, or `None` | A tuple of (stringified IPv4 or IPv6 address, integer port number); can be `None` in connected mode.

addressFamily: `socket.AddressFamily` = (source)
overrides `twisted.internet.base.BasePort.addressFamily`

`socket.AF_INET` or `socket.AF_INET6`, depending on whether this port is listening on an IPv4 address or an IPv6 address.

connected: `int` = (source)
overrides `twisted.internet.abstract.FileDescriptor.connected`
overridden in `twisted.internet.unix.DatagramPort`

Undocumented

d = (source)

Undocumented

interface = (source)

Undocumented

logstr = (source)
overridden in `twisted.internet.unix.DatagramPort`

Undocumented

maxPacketSize = (source)

Undocumented

maxThroughput: `int` = (source)

Maximum number of bytes read in one event loop iteration.

port = (source)

Undocumented

protocol = (source)

Undocumented

socket = (source)
overridden in `twisted.internet.unix.DatagramPort`

Undocumented

@classmethod
def _fromListeningDescriptor(cls, reactor, fd, addressFamily, protocol, maxPacketSize): (source)

Create a new `Port` based on an existing listening SOCK_DGRAM socket.

Parameters
reactor:`interfaces.IReactorFDSet` | A reactor which will notify this `Port` when its socket is ready for reading or writing. Defaults to `None`, ie the default global reactor.
fd:`int` | An integer file descriptor associated with a listening socket. The socket must be in non-blocking mode. Any additional attributes desired, such as FD_CLOEXEC, must also be set already.
addressFamily:`int` | The address family (sometimes called domain) of the existing socket. For example, `socket.AF_INET`.
protocol:`twisted.internet.protocol.DatagramProtocol` | A `DatagramProtocol` instance which will be connected to the `port`.
maxPacketSize:`int` | The maximum packet size to accept.

Returns
`Port` | A new instance of `cls` wrapping the socket given by `fd`.

def _bindSocket(self): (source)
overridden in `twisted.internet.unix.DatagramPort`

Prepare and assign a `socket.socket` instance to `self.socket`.

Either creates a new SOCK_DGRAM `socket.socket` bound to `self.interface` and `self.port` or takes an existing `socket.socket` provided via the `interfaces.IReactorSocket.adoptDatagramPort` interface.

def _connectToProtocol(self): (source)

Undocumented

def _loseConnection(self): (source)

Undocumented

def _setAddressFamily(self): (source)

Resolve address family for the socket.

_connectedAddr = (source)

Undocumented

_preexistingSocket = (source)

If not `None`, a `socket.socket` instance which was created and initialized outside of the reactor and will be used to listen for connections (instead of a new socket being created by this `Port`).

_realPortNumber: `int | None` = (source)

Actual port number being listened on. The value will be `None` until this `Port` is listening.