Low-Level ServerConnection() Interface

class memcached2.ServerConnection(uri)

Low-level communication with the memcached server.

Data should be passed in as strings, and that is converted to bytes for sending to the backend, encoded as ASCII, if necessary. Data returned is likewise converted from bytes, also encoded as ASCII, if necessary.

This implments the connection to a server, sending messages and reading responses. This is largely intended to be used by other modules in the memcached2 module such as Memcache() rather than directly by consumers.

Note that this class buffers data read from the server, so you should never read data directly from the underlying socket, as it may confuse other software which uses this interface.

Parameters:uri (str) – The URI of the backend server.
connect()

Connect to memcached server.

If already connected, this function returns immmediately. Otherwise, the connection is reset and a connection is made to the backend.

Raises:UnknownProtocol
consume_from_buffer(length)

Retrieve the specified number of bytes from the buffer.

Parameters:length (int) – Number of bytes of data to consume from buffer.
Returns:str – Data from buffer.
parse_uri()

Parse a server connection URI.

Parses the uri attribute of this object.

Currently, the only supported URI format is of the form:

  • memcached://<hostname>[:port]/ – A TCP socket connection to the host, optionally on the specified port. If port is not specified, port 11211 is used.
Returns:dict – A dictionary with the key protocol and other protocol-specific keys. For memcached protocol the keys include host, and port.
Raises:InvalidURI
read_length(length)

Read the specified number of bytes.

Parameters:length (int) – Number of bytes of data to read.
Returns:str – Data read from socket. Converted from bytes (as read from backend) with ASCII encoding, if necessary.
Raises:ServerDisconnect
read_until(search='\r\n')

Read data from the server until “search” is found.

Data is read in blocks, any remaining data beyond search is held in a buffer to be consumed in the future.

param search:Read data from the server until search is found. This defaults to ‘
‘, so it acts like readline().
type search:str
returns:str – Data read, up to and including search. Converted from bytes (as read from backend) with ASCII encoding, if necessary.
raises:ServerDisconnect
reset()

Reset the connection.

Flushes buffered data and closes the backend connection.

send_command(command)

Write an ASCII command to the memcached server.

Parameters:command (str) – Data that is sent to the server. This is converted to a bytes type with ASCII encoding if necessary for sending across the socket.
Raises:ServerDisconnect