{"id":10157,"library":"python-binary-memcached","title":"Python Binary Memcached Client","description":"A pure Python module for accessing Memcached servers using the binary protocol, including support for SASL authentication and TLS. The current version is 0.31.4, with releases occurring periodically to add features and improvements.","status":"active","version":"0.31.4","language":"en","source_language":"en","source_url":"https://github.com/jaysonsantos/python-binary-memcached","tags":["memcached","caching","binary protocol","SASL","TLS","client"],"install":[{"cmd":"pip install python-binary-memcached","lang":"bash","label":"Install core library"},{"cmd":"pip install python-binary-memcached[sasl]","lang":"bash","label":"Install with SASL authentication support"}],"dependencies":[],"imports":[{"note":"The primary client for interacting with a single or failover Memcached setup.","wrong":"import bmemcached.Client","symbol":"Client","correct":"from bmemcached import Client"},{"note":"A client for distributing keys across multiple Memcached servers using consistent hashing.","symbol":"DistributedClient","correct":"from bmemcached import DistributedClient"}],"quickstart":{"code":"import bmemcached\nimport os\n\n# Configure client with Memcached server address, optional username, and password\n# Use environment variables for sensitive information\nservers = ['127.0.0.1:11211']\nusername = os.environ.get('MEMCACHED_USER', '')\npassword = os.environ.get('MEMCACHED_PASSWORD', '')\n\nmc = bmemcached.Client(servers, username, password)\n\n# Set a key-value pair\nmc.set('my_key', 'Hello, Memcached!')\nprint(f\"Set 'my_key': {mc.get('my_key')}\")\n\n# Get a key-value pair\nvalue = mc.get('my_key')\nif value:\n    print(f\"Retrieved 'my_key': {value.decode('utf-8')}\")\nelse:\n    print(\"'my_key' not found\")\n\n# Delete a key\nmc.delete('my_key')\nprint(f\"Deleted 'my_key'. Now 'my_key' is: {mc.get('my_key')}\")","lang":"python","description":"Initializes a `bmemcached.Client` instance, sets a string value, retrieves it, and then deletes it. Ensure a Memcached server is running at `127.0.0.1:11211`."},"warnings":[{"fix":"Install `pysasl` manually via `pip install pysasl` or use the extra: `pip install python-binary-memcached[sasl]`.","message":"SASL authentication requires the `pysasl` library, which is an optional dependency. If you use SASL and encounter errors like `AttributeError: 'NoneType' object has no attribute 'init_client'` or `SASL error: -4`, `pysasl` is likely missing.","severity":"gotcha","affected_versions":"All versions when using SASL"},{"fix":"If consistent hashing is desired for load balancing and distribution, instantiate `bmemcached.DistributedClient` instead of `bmemcached.Client`.","message":"For distributing keys across multiple Memcached servers using consistent hashing, explicitly use `bmemcached.DistributedClient`. While `bmemcached.Client` can take multiple servers, it primarily uses a failover strategy (it's internally a `ReplicantClient` since v0.28.0) rather than distributing keys.","severity":"gotcha","affected_versions":"v0.28.0 and later"},{"fix":"When initializing `Client`, pass a dictionary to the `tls_params` argument, e.g., `tls_params={'ca_certs': 'path/to/ca.pem'}`.","message":"TLS support was added in v0.29.0. To establish a secure connection, you must explicitly pass `tls_params` to the client constructor. Connections made without `tls_params` will not be encrypted.","severity":"gotcha","affected_versions":"Pre-v0.29.0 behavior, relevant for v0.29.0+"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Run `pip install python-binary-memcached` to install the library.","cause":"The `python-binary-memcached` library is not installed in your current Python environment.","error":"ModuleNotFoundError: No module named 'bmemcached'"},{"fix":"Install the `pysasl` library: `pip install pysasl` or `pip install python-binary-memcached[sasl]`.","cause":"The `pysasl` library, an optional dependency required for SASL authentication, is not installed.","error":"AttributeError: 'NoneType' object has no attribute 'init_client' (during client initialization with SASL) OR SASL error: -4 (SASL not initialized)"},{"fix":"Verify that your Memcached server is running, listening on the correct IP address and port (e.g., `127.0.0.1:11211`), and that no firewall rules are preventing your application from connecting.","cause":"The Memcached server is either not running, not accessible at the specified host/port, or a firewall is blocking the connection.","error":"socket.error: [Errno 111] Connection refused OR ConnectionError: [Errno 111] Connection refused"}]}