HEX
Server: LiteSpeed
System: Linux cpir1.prohostdns.com 4.18.0-553.123.2.lve.el8.x86_64 #1 SMP Thu May 7 23:17:13 UTC 2026 x86_64
User: pelakir (2976)
PHP: 8.2.31
Disabled: exec, shell_exec, system, passthru, proc_open, proc_close, proc_terminate, proc_get_status, popen, pclose, pcntl_exec
Upload Files
File: //usr/local/lib/python3.6/site-packages/bmemcached/__pycache__/protocol.cpython-36.pyc
3

�':f���@s�ddlmZmZddlZddlZddlZddlZddlZyddlmZWn e	k
rhddl
mZYnXddlZddlm
Z
ddlZddlmZmZddlmZddlmZmZmZddlmZeje�ZGd	d
�d
ej�ZdS)�)�datetime�	timedeltaN)�	splitport)�BytesIO)�binary_type�	text_type)�long)�AuthenticationNotSupported�InvalidCredentials�MemcachedException)�str_to_bytescs�eZdZdZdZdZddd�Zddd	�d
dd	�ddd	�dd
d	�dd
d	�dd
d	�dd
d	�dd
d	�ddd	�ddd	�ddd	�ddd	�ddd	�ddiddiddd	�d�Zdddddd d!�Zdedfdgdhdid"�Z	d#Z
dZdj�fd%d&�	Zd'd(�Z
ed)d*��Zd+d,�Zd-d.�Zd/d0�Zed1d2��Zd3d4�Zd5d6�Zd7d8�Zd9d:�Zd;d<�Zdld=d>�Zd?d@�ZdAdB�ZdCdD�ZdEdF�ZdndGdH�ZdpdIdJ�Z drdKdL�Z!dtdMdN�Z"dvdOdP�Z#dxdRdS�Z$dTdU�Z%dydWdX�Z&dzdYdZ�Z'd{d[d\�Z(d]d^�Z)d_d`�Z*d|dadb�Z+dcdd�Z,�Z-S)}�ProtocolaD
    This class is used by Client class to communicate with server.

    Reference https://github.com/memcached/memcached/wiki/BinaryProtocolRevamped ::

        Header structure
        Byte/     0       |       1       |       2       |       3       |
           /              |               |               |               |
          |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
          +---------------+---------------+---------------+---------------+
         0| Magic         | Opcode        | Key length                    |
          +---------------+---------------+---------------+---------------+
         4| Extras length | Data type     | vbucket id                    |
          +---------------+---------------+---------------+---------------+
         8| Total body length                                             |
          +---------------+---------------+---------------+---------------+
        12| Opaque                                                        |
          +---------------+---------------+---------------+---------------+
        16| CAS                                                           |
          |                                                               |
          +---------------+---------------+---------------+---------------+
          Total 24 bytes
    z
!BBHBBHLLQ���)�request�responserz%ds)�command�struct��
�zLL%ds%ds������zQQL%ds���I�
�r�� �!z%ds%ds)�get�getk�getkq�set�setq�add�addq�replace�delete�incr�decr�flush�noop�stat�auth_negotiation�auth_requestl��)�success�
key_not_found�
key_exists�
auth_error�unknown_command�server_disconnected)�object�integerr�
compressed�binaryl��Nc

s�tt|�j�||_||_||_|dkr,tn||_d|_d|_	||_
||_||_||_
|	|_d|_|jd�s�|j|j�\|_|_|jd�nd|_|_|jd�dS)NF�/rr)�superr
�__init__�server�	_username�	_password�zlib�compression�
connection�
authenticated�socket_timeout�pickle_protocol�pickler�	unpickler�tls_context�reconnects_deferred_until�
startswith�split_host_port�host�port�set_retry_delay)
�selfrC�username�passwordrGrJrKrLrMrN)�	__class__��F/tmp/pip-build-5ba02zx6/python-binary-memcached/bmemcached/protocol.pyrBes$
zProtocol.__init__cCsdj|j|j|j�S)Nz{}_{}_{})�formatrCrDrE)rUrYrYrZ�__str__~szProtocol.__str__cCs
|jdkS)N)rR)rUrYrYrZ�server_uses_unix_socket�sz Protocol.server_uses_unix_socketcCs
||_dS)N)�retry_delay)rU�valuerYrYrZrT�szProtocol.set_retry_delaycCs�|jr
dSd|_|jr(|jtj�kr(dSy�|jr�tjtjtj�|_|jj	|j
�|jj|j|jf�|j
r�|j
j|j|jd�|_n tjtjtj�|_|jj|j�|j�Wn.tjk
r�tj�t|jd�|_�YnXdS)NF)�server_hostname)�seconds)rHrIrOr�nowrR�socket�AF_INET�SOCK_STREAM�
settimeoutrJ�connectrSrN�wrap_socket�AF_UNIXrC�_send_authentication�errorrr^)rUrYrYrZ�_open_connection�s(zProtocol._open_connectioncCs|j�dS)N)�
disconnect)rU�	exceptionrYrYrZ�_connection_error�szProtocol._connection_errorcCsBt|�\}}|dkrd}t|�}tjd|�r:tjdd|�}||fS)z�
        Return (host, port) from server.

        Port defaults to 11211.

        >>> split_host_port('127.0.0.1:11211')
        ('127.0.0.1', 11211)
        >>> split_host_port('127.0.0.1')
        ('127.0.0.1', 11211)
        Ni�+z:.*$r")r�int�re�search�sub)�clsrCrRrSrYrYrZrQ�szProtocol.split_host_portcCsVt�}x2t|�|kr8|jj|t|��}|s.P||7}qWt|�|krNtj��t|�S)z{
        Reads data from socket.

        :param size: Size in bytes to be read.
        :return: Data from socket
        )�	bytearray�lenrH�recvrcrk�bytes)rU�sizer_�datarYrYrZ�_read_socket�szProtocol._read_socketcCs�y�|j�|jdkrtjd��|j|j�}tj|j|�\	}}}}}}}}	}
||j	dks\t
�d}|rn|j|�}||||||||	|
|f
Stjk
r�}z6|j|�t|�}
|j	ddddd|j
dddd|
f
Sd}~XnXdS)z�
        Get memcached response from socket.

        :return: A tuple with binary values from memcached.
        :rtype: tuple
        NzDelaying reconnection attemptrrrr;���)rlrHrcrkr{�HEADER_SIZEr�unpack�
HEADER_STRUCT�MAGIC�AssertionErrorro�str�STATUS)rU�header�magic�opcode�keylen�extlen�datatype�status�bodylen�opaque�cas�
extra_content�e�messagerYrYrZ�
_get_response�s 

 


zProtocol._get_responsecCsZy&|j�|jdkrdS|jj|�Wn.tjk
rT}z|j|�WYdd}~XnXdS)N)rlrH�sendallrcrkro)rUrzr�rYrYrZ�_send�s
zProtocol._sendcCs"||_||_|j�|j�|jS)a�
        Authenticate user on server.

        :param username: Username used to be authenticated.
        :type username: six.string_types
        :param password: Password used to be authenticated.
        :type password: six.string_types
        :return: True if successful.
        :raises: InvalidCredentials, AuthenticationNotSupported, MemcachedException
        :rtype: bool
        )rDrErmrlrI)rUrVrWrYrYrZ�authenticate�s
zProtocol.authenticatecCs�|js|jrdStjd|j�|jtj|j|jd|j	ddddddddd�
�|j
�\
}}}}}}}}}	}
||jdkr�dS||jdkr�tjd	�d
|_d
S|
}d|kr�t
d|��d}d
|j|jf}
t|
t�r�|
j�}
|jtj|j|j	ddt|�t|
�f|jd|j	ddt|�dddt|�t|
�dd||
��|j
�\
}}}}}}}}}	}
||jdk�rxdS||jdk�r�td|��||jdk�r�td||
f|��tjd||
�d
|_d
S)NFzAuthenticating as %srr4rrr;r:z(Server does not requires authentication.TsPLAINz-This module only supports PLAIN auth for now.z%s%sr5rr9zIncorrect username or passwordr6zCode: %d Message: %szAuth OK. Code: %d Message: %s)rDrE�logger�debugr�r�packrr��COMMANDSr�r�rIr	�
isinstancer�encodervr
r)rUr�r�r�r�r�r�r�r�r�r��methods�method�authrYrYrZrjsH

(
zProtocol._send_authenticationcCs@d}t|t�r||jdO}n�t|t�r4|jd�}n�t|t�rdt|t�dkrd||jdO}t|�}ndt|t�r�t|t�dkr�||jdO}t|�}n4||jdO}t	�}|j
||j�}|j|�|j
�}|dko�t|�|jk�r8|dk	o�|dk�r|jj||�}n|jj|�}|�r8t|�t|�k�r8|}||jd	O}||fS)
a�
        Serializes a value based on its type.

        :param value: Something to be serialized
        :type value: six.string_types, int, long, object
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: Serialized type
        :rtype: str
        rr?�utf8Fr=rr<Nr>)r�r�FLAGSrr�rp�boolr�rrrLrK�dump�getvaluerv�COMPRESSION_THRESHOLDrG�compress)rUr_�compress_level�flags�bufrLZcompressed_valuerYrYrZ�	serializeIs0





zProtocol.serializecCs�|j}||d@r|jj|�}||d@r.|S||d@rBt|�S||d@rVt|�S||d@r|t|�}|j|�}|j�Stj	r�|j
d�Sy|j
d�Wn4tk
r�y
|j
d�Stk
r�|SXYnX|SdS)	a>
        Deserialized values based on flags or just return it if it is not serialized.

        :param value: Serialized or not value.
        :type value: six.string_types, int
        :param flags: Value flags
        :type flags: int
        :return: Deserialized value
        :rtype: six.string_types|int
        r>r?r=rr<r��asciiN)r�rG�
decompressrprrrM�load�six�PY3�decode�UnicodeDecodeError)rUr_r�r�r�rMrYrYrZ�deserializevs.


zProtocol.deserializecCstjd|�t|�}tj|j|jddt|�f|jd|jddt|�dddt|�dd|�}|j	|�|j
�\
}}}}}}	}
}}}
tjd||
|�|	|jdkr�|	|jd	kr�tjd
|
�dS|	|jdkr�dStd
|	|
f|	��tj
d|
df|
�\}}|j||�|fS)z�
        Get a key and its CAS value from server.  If the value isn't cached, return
        (None, None).

        :param key: Key's name
        :type key: six.string_types
        :return: Returns (value, cas).
        :rtype: object
        zGetting key %sr&rrrrz0Value Length: %d. Body length: %d. Data type: %dr6r7zKey not found. Message: %sNr;zCode: %d Message: %sz!L%dsr)NN)NN)r�r�rrr�rr�rvr�r�r�r�rr~r�)rU�key�keybytesrzr�r�r�r�r�r�r�r�r�r�r�r_rYrYrZr&�s(


zProtocol.getcCs�tjd�tj|j|jdd|jd|jddddddddd�
}|j|�|j�\
}}}}}}}}	}
}tjd|||�||j	dkr�tjd	||f�t
|�S)
z_
        Send a NOOP command

        :return: Returns the status.
        :rtype: int
        zSending NOOPr2rrrrz0Value Length: %d. Body length: %d. Data type: %dr6z'NOOP failed (status is %d). Message: %s)r�r�rr�rr�r�r�r�r�rp)rUrzr�r�r�r�r�r�r�r�r�r�rYrYrZr2�s


z
Protocol.noopcCs�t|�}|dkriSt�}x~t|�D]r\}}t|�}|j||dkrHdnd}|tj|j|dt|�f|jd|dt|�dddt|�dd|�7}q$W|j	|�i}d}	x�|	|jddk�r\|j
�\
}
}	}}}
}}}}}||jdk�r&tjd	|||d
f|�\}}}|j
||�|f||<q�||jdk�r:Pq�||jdkr�td
||f|��q�Wi}x,|D]$}t|�}||k�rh||||<�qhW|S)ai
        Get multiple keys from server.

        Since keys are converted to b'' when six.PY3 the keys need to be decoded back
        into string . e.g key='test' is read as b'test' and then decoded back to 'test'
        This encode/decode does not work when key is already a six.binary_type hence
        this function remembers which keys were originally sent as str so that
        it only decoded those keys back to string which were sent as string

        :param keys: A list of keys to from server.
        :type keys: Collection
        :return: A dict with all requested keys.
        :rtype: dict
        rrr'r(rrrr6z!L%ds%dsrr;r7zCode: %d Message: %sr|)rvru�	enumeraterr�rr�rr�r�r�r�r~r�r)rU�keys�n�msg�ir�r�r�dr�r�r�r�r�r�r�r�r�r�r�r_�retrYrYrZ�	get_multi�s@
"


zProtocol.get_multicCsH|dkr|n|j}tjd|�|j||d�\}}tjdt|��t|t�rT|jd�}t|�}|j	t
j|j|j
|dt|�t|�f|jd|j
|dt|�d	ddt|�t|�d	d|||||��|j�\
}	}
}}}
}}}}}||jd
k�rD||jdk�r
dS||jd
k�rdS||jdk�r2dStd||f|��dS)a�
        Function to set/add/replace commands.

        :param key: Key's name
        :type key: six.string_types
        :param value: A value to be stored on server.
        :type value: object
        :param time: Time in seconds that your key will expire.
        :type time: int
        :param cas: The CAS value that must be matched for this operation to complete, or 0 for no CAS.
        :type cas: int
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: True in case of success and False in case of failure
        :rtype: bool
        rz Setting/adding/replacing key %s.)r�zValue bytes %s.r�rrrrr6r8Fr7r;zCode: %d Message: %sT)�MAXIMUM_EXPIRE_TIMEr�r�r�rvr�rr�rr�rr�rr�r�r�r�r)rUrr�r_�timer�r�r�r�r�r�r�r�r�r�r�r�r�rYrYrZ�_set_add_replaces.

$zProtocol._set_add_replacecCs|jd||||d�S)aA
        Set a value for a key on server.

        :param key: Key's name
        :type key: six.string_types
        :param value: A value to be stored on server.
        :type value: object
        :param time: Time in seconds that your key will expire.
        :type time: int
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: True in case of success and False in case of failure
        :rtype: bool
        r))r�)r�)rUr�r_r�r�rYrYrZr)LszProtocol.setcCsF|dkstd��|dkr,|jd||||d�S|jd|||||d�SdS)ah
        Add a key/value to server ony if it does not exist.

        :param key: Key's name
        :type key: six.string_types
        :param value: A value to be stored on server.
        :type value: object
        :param time: Time in seconds that your key will expire.
        :type time: int
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: True if key is added False if key already exists and has a different CAS
        :rtype: bool
        rz0 is an invalid CAS valueNr+)r�r))r�r�)r�r�)rUr�r_r�r�r�rYrYrZr�_szProtocol.cascCs|jd||||d�S)aP
        Add a key/value to server ony if it does not exist.

        :param key: Key's name
        :type key: six.string_types
        :param value: A value to be stored on server.
        :type value: object
        :param time: Time in seconds that your key will expire.
        :type time: int
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: True if key is added False if key already exists
        :rtype: bool
        r+)r�)r�)rUr�r_r�r�rYrYrZr+|szProtocol.addcCs|jd||||d�S)aS
        Replace a key/value to server ony if it does exist.

        :param key: Key's name
        :type key: six.string_types
        :param value: A value to be stored on server.
        :type value: object
        :param time: Time in seconds that your key will expire.
        :type time: int
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: True if key is replace False if key does not exists
        :rtype: bool
        r-)r�)r�)rUr�r_r�r�rYrYrZr-�szProtocol.replace�dcCs�t|j��}t�}x�t|�D]�\}\}}t|t�r<|\}}nd}|dkrNd}	nd}	t|�}
|j||d�\}}|tj	|j
|j|	dt|
�t|�f|j
d|j|	dt|
�d	ddt|
�t|�d	||p�d|||
|�7}qW|tj	|j
|jd
d|j
d|jd
dddddddd�
7}|j|�d}g}
x�||jd
dk�r�|j�\
}}}}}}}}}}||jdk�r�td
d�|D��S||jdk�r4||\}}t|t��r�|
j|d|f�n
|
j|��q4W|
S)aY
        Set multiple keys with its values on server.

        If a key is a (key, cas) tuple, insert as if cas(key, value, cas) had
        been called.

        :param mappings: A dict with keys/values
        :type mappings: dict
        :param time: Time in seconds that your key will expire.
        :type time: int
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: List of keys that failed to be set.
        :rtype: list
        Nrr,r*)r�rrrrr2rr;css|]\}}|VqdS)NrY)�.0r�r_rYrYrZ�	<genexpr>�sz%Protocol.set_multi.<locals>.<genexpr>r6r|)�list�itemsrur�r��tuplerr�rr�rr�rvr�r�r�r��append)rUZmappingsr�r�r�r�r�r_r�rr�r�r��failedr�r�r�r�r�r�r�rYrYrZ�	set_multi�sH


 

zProtocol.set_multicCs�t|�}|dkr|n|j}|jtj|j|j|dt|�|jd|j|dt|�dddt|�ddd||||��|j	�\
}}}	}
}}}
}}}||j
d|j
dfkr�td||f|��||j
dkr�dStjd	|�dS)
a�
        Function which increments and decrements.

        :param key: Key's name
        :type key: six.string_types
        :param value: Number to be (de|in)cremented
        :type value: int
        :param default: Default value if key does not exist.
        :type default: int
        :param time: Time in seconds to expire key.
        :type time: int
        :return: Actual value of the key on server
        :rtype: int
        rrrr�r6r;zCode: %d Message: %sz!Q)
rr�r�rr�rr�rvr�r�r�rr~)rUrr�r_�defaultr�r�r�r�r�r�r�r�r�r�r�r�rYrYrZ�
_incr_decr�szProtocol._incr_decr�@BcCs|jd||||�S)a�
        Increment a key, if it exists, returns its actual value, if it doesn't, return 0.

        :param key: Key's name
        :type key: six.string_types
        :param value: Number to be incremented
        :type value: int
        :param default: Default value if key does not exist.
        :type default: int
        :param time: Time in seconds to expire key.
        :type time: int
        :return: Actual value of the key on server
        :rtype: int
        r/)r�)rUr�r_r�r�rYrYrZr/
sz
Protocol.incrcCs|jd||||�S)a

        Decrement a key, if it exists, returns its actual value, if it doesn't, return 0.
        Minimum value of decrement return is 0.

        :param key: Key's name
        :type key: six.string_types
        :param value: Number to be decremented
        :type value: int
        :param default: Default value if key does not exist.
        :type default: int
        :param time: Time in seconds to expire key.
        :type time: int
        :return: Actual value of the key on server
        :rtype: int
        r0)r�)rUr�r_r�r�rYrYrZr0sz
Protocol.decrc

Cs�tjd|�t|�}|jtj|j|jddt|�f|j	d|jddt|�dddt|�d||��|j
�\
}}}}}}	}
}}}|	|jdkr�dS|	|jd	kr�|	|jd
|jdfkr�td|	|f|	��tjd
|�|	|jdkS)al
        Delete a key/value from server. If key existed and was deleted, return True.

        :param key: Key's name to be deleted
        :type key: six.string_types
        :param cas: If set, only delete the key if its CAS value matches.
        :type cas: int
        :return: True in case o success and False in case of failure.
        :rtype: bool
        zDeleting key %sr.rrrrr;Fr6r7r8zCode: %d message: %szKey deleted %s)
r�r�rr�rr�rr�rvr�r�r�r)
rUr�r�r�r�r�r�r�r�r�r�r�r�rYrYrZr.-s&zProtocol.deletecCs,tjd|�t�}xh|D]`}t|�}|tj|j|jddt|�f|j	d|jddt|�dddt|�dd|�7}qW|tj|j|jdd|j	d|jddddddddd�
7}|j
|�d
}d	}xX||jddk�r&|j�\
}}}}	}
}}}
}}||jd
k�rd}||jdkr�Pq�W|S)z�
        Delete multiple keys from server in one command.

        :param keys: A list of keys to be deleted
        :type keys: list
        :return: True in case of success and False in case of failure.
        :rtype: bool
        zDeleting keys %rr.rrrrr2rTr6Fr;r|)
r�r�rurrr�rr�rvr�r�r�r�)rUr�r�r�r�r��retvalr�r�r�r�r�r�r�r�r�rYrYrZ�delete_multiKs4	
"
zProtocol.delete_multic
Cs�tjd�|jtj|j|jdd|jd|jddddddddd|��|j�\
}}}}}}}}	}
}||j	d|j	d	fkr�t
d
||f|��tjd�dS)
z�
        Send a command to server flush|delete all keys.

        :param time: Time to wait until flush in seconds.
        :type time: int
        :return: True in case of success, False in case of failure
        :rtype: bool
        zFlushing memcachedr1rrrrrr6r;zCode: %d message: %szMemcached flushedT)r��infor�rr�rr�r�r�r�rr�)rUr�r�r�r�r�r�r�r�r�r�r�rYrYrZ�	flush_allts	

zProtocol.flush_allc
Cs |dk	r\t|t�rt|�}t|�}tj|jd||jd|jdd|ddd|dd|�}n.tj|j|jd|jddddddddd�
}|j	|�i}x�|j
�}|d}||jdkr�P|d	}|d
}|dkr�|dkr�P|d}|d|�}|||�}	|	|t|t��r|j
�n|<q�W|S)
z�
        Return server stats.

        :param key: Optional if you want status from a key.
        :type key: six.string_types
        :return: A dict with server stats
        :rtype: dict
        Nz%dsrr3rrrr;rrrr|)r�rrrvrr�rr�r�r�r�r�rxr�)
rUr�r��packedr_rr�r�r��bodyrYrYrZ�stats�s<


 zProtocol.statscCs|jr|jj�d|_dS)z�
        Disconnects from server.  A new connection will be established the next time a request is made.

        :return: Nothing
        :rtype: None
        N)rH�close)rUrYrYrZrm�s
zProtocol.disconnectrrrrr#)NNNNNNNNr|)r|r|)rr|r|)r|r|)r|r|)r|r|)r|r|)r�r|)rr�)rr�)r)N).�__name__�
__module__�__qualname__�__doc__rr}r�r�r�r�r�r�rBr\�propertyr]rTrlro�classmethodrQr{r�r�r�rjr�r�r&r2r�r�r)r�r+r-r�r�r/r0r.r�r�r�rm�
__classcell__rYrY)rXrZr
s�"
6
-,'9
0




E#


)
2r
)rr�loggingrqrcr�	threading�urllibr�ImportError�urllib.parserF�iorr�rrZbmemcached.compatrZbmemcached.exceptionsr	r
rZbmemcached.utilsr�	getLoggerr�r��localr
rYrYrYrZ�<module>s$