class NetstringReceiver(protocol.Protocol): (source)
A protocol that sends and receives netstrings.
See http://cr.yp.to/proto/netstrings.txt for the specification of netstrings. Every netstring starts with digits that specify the length of the data. This length specification is separated from the data by a colon. The data is terminated with a comma.
Override stringReceived
to handle received netstrings. This method is called with the netstring payload as a single argument whenever a complete netstring is received.
Security features:
- Messages are limited in size, useful if you don't want someone sending you a 500MB netstring (change self.MAX_LENGTH to the maximum length you wish to accept).
- The connection is lost if an illegal message is received.
Method | data |
Receives some characters of a netstring. |
Method | make |
Initializes the protocol. |
Method | send |
Sends a netstring. |
Method | string |
Override this for notification when each complete string is received. |
Constant | MAX |
Defines the maximum length of netstrings that can be received. |
Instance Variable | broken |
Indicates if the connection is still functional |
Method | _check |
Checks if the netstring has a trailing comma at the expected position. |
Method | _check |
Makes sure that the received data represents a valid number. |
Method | _check |
Checks the sanity of lengthAsString. |
Method | _consume |
Consumes the content of self._remainingData. |
Method | _consume |
Consumes the length portion of self._remainingData. |
Method | _consume |
Consumes the payload portion of self._remainingData. |
Method | _extract |
Attempts to extract the length information of a netstring. |
Method | _extract |
Extracts payload information from self._remainingData. |
Method | _handle |
Terminates the connection and sets the flag self.brokenPeer. |
Method | _max |
Calculate and return the string size of self.MAX_LENGTH. |
Method | _payload |
Checks if enough data have been received to complete the netstring. |
Method | _prepare |
Sets up variables necessary for consuming the payload of a netstring. |
Method | _process |
Processes the length definition of a netstring. |
Method | _process |
Processes the actual payload with stringReceived . |
Constant | _LENGTH |
A pattern describing all strings that contain a netstring length specification. Examples for length specifications are b'0:', b'12:', and b'179:'. b'007:' is not a valid length specification, since leading zeros are not allowed. |
Constant | _LENGTH |
A pattern describing all strings that contain the first part of a netstring length specification (without the trailing comma). Examples are '0', '12', and '179'. '007' does not start a netstring length specification, since leading zeros are not allowed. |
Constant | _MISSING |
Undocumented |
Constant | _MISSING |
Undocumented |
Constant | _OVERFLOW |
Undocumented |
Constant | _TOO |
Undocumented |
Instance Variable | _current |
Undocumented |
Instance Variable | _expected |
Holds the payload size plus one for the trailing comma. |
Instance Variable | _PARSING |
Indicates that the NetstringReceiver is in the state of parsing the length portion of a netstring. |
Instance Variable | _PARSING |
Indicates that the NetstringReceiver is in the state of parsing the payload portion (data and trailing comma) of a netstring. |
Instance Variable | _payload |
Holds the payload portion of a netstring including the trailing comma |
Instance Variable | _remaining |
Holds the chunk of data that has not yet been consumed |
Instance Variable | _state |
Indicates if the protocol is consuming the length portion (PARSING_LENGTH) or the payload (PARSING_PAYLOAD) of a netstring |
Inherited from Protocol
:
Method | connection |
Called when the connection is shut down. |
Method | log |
Return a prefix matching the class name, to identify log messages related to this protocol instance. |
Class Variable | factory |
Undocumented |
Inherited from BaseProtocol
(via Protocol
):
Method | connection |
Called when a connection is made. |
Instance Variable | connected |
Undocumented |
Instance Variable | transport |
Undocumented |
Receives some characters of a netstring.
Whenever a complete netstring is received, this method extracts its payload and calls stringReceived
to process it.
Parameters | |
data:bytes | A chunk of data representing a (possibly partial) netstring |
Sends a netstring.
Wraps up string by adding length information and a trailing comma; writes the result to the transport.
Parameters | |
string:bytes | The string to send. The necessary framing (length prefix, etc) will be added. |
Override this for notification when each complete string is received.
Parameters | |
string:bytes | The complete string which was received with all framing (length prefix, etc) removed. |
Raises | |
NotImplementedError | because the method has to be implemented by the child class. |
Checks if the netstring has a trailing comma at the expected position.
Raises | |
NetstringParseError | if the last payload character is anything but a comma. |
Makes sure that the received data represents a valid number.
Checks if self._remainingData represents a number smaller or equal to self.MAX_LENGTH.
Raises | |
NetstringParseError | if self._remainingData is no number or is too big (checked by _extractLength ). |
Checks the sanity of lengthAsString.
Checks if the size of the length specification exceeds the size of the string representing self.MAX_LENGTH. If this is not the case, the number represented by lengthAsString is certainly bigger than self.MAX_LENGTH, and a NetstringParseError can be raised.
This method should make sure that netstrings with extremely long length specifications are refused before even attempting to convert them to an integer (which might trigger a MemoryError).
Consumes the content of self._remainingData.
Raises | |
IncompleteNetstring | if self._remainingData does not contain enough data to complete the current netstring. |
NetstringParseError | if the received data do not form a valid netstring. |
Consumes the length portion of self._remainingData.
Raises | |
IncompleteNetstring | if self._remainingData contains a partial length specification (digits without trailing comma). |
NetstringParseError | if the received data do not form a valid netstring. |
Consumes the payload portion of self._remainingData.
If the payload is complete, checks for the trailing comma and processes the payload. If not, raises an IncompleteNetstring
exception.
Raises | |
IncompleteNetstring | if the payload received so far contains fewer characters than expected. |
NetstringParseError | if the payload does not end with a comma. |
Attempts to extract the length information of a netstring.
Parameters | |
length | A chunk of data starting with a length specification |
Returns | |
int | The length of the netstring |
Raises | |
NetstringParseError | if the number is bigger than self.MAX_LENGTH. |
Extracts payload information from self._remainingData.
Splits self._remainingData at the end of the netstring. The first part becomes self._payload, the second part is stored in self._remainingData.
If the netstring is not yet complete, the whole content of self._remainingData is moved to self._payload.
Calculate and return the string size of self.MAX_LENGTH.
Returns | |
float | The size of the string representation for self.MAX_LENGTH |
Checks if enough data have been received to complete the netstring.
Returns | |
bool | True iff the received data contain at least as many characters as specified in the length section of the netstring |
Processes the length definition of a netstring.
Extracts and stores in self._expectedPayloadSize the number representing the netstring size. Removes the prefix representing the length specification from self._remainingData.
Parameters | |
length | A regular expression match object matching a netstring length specification |
Raises | |
NetstringParseError | if the received netstring does not start with a number or the number is bigger than self.MAX_LENGTH. |
Processes the actual payload with stringReceived
.
Strips self._payload of the trailing comma and calls stringReceived
with the result.
A pattern describing all strings that contain a netstring length specification. Examples for length specifications are b'0:', b'12:', and b'179:'. b'007:' is not a valid length specification, since leading zeros are not allowed.
Value |
|
A pattern describing all strings that contain the first part of a netstring length specification (without the trailing comma). Examples are '0', '12', and '179'. '007' does not start a netstring length specification, since leading zeros are not allowed.
Value |
|
Undocumented
Value |
|
Undocumented
Value |
|
Indicates that the NetstringReceiver is in the state of parsing the payload portion (data and trailing comma) of a netstring.