Connecting to Memcached on the Python-binary-memcached (Python)
Access a DCS Memcached instance using Python on an ECS in the same VPC.
Prerequisites
- The DCS Memcached instance you want to access is in the Running state.
- Log in to the ECS. For details on how to create ECSs, see the Elastic Cloud Server User Guide.
An ECS can communicate with a DCS instance that belongs to the same VPC and is configured with the same security group.
- If the ECS and DCS instance are not in the same VPC, you can establish a VPC peering connection to achieve network connectivity between the ECS and DCS instance. For details, see Does DCS Support Cross-VPC Access?
- If different security groups have been configured for the ECS and DCS instance, you can set security group rules to achieve network connectivity between the ECS and DCS instance. For details, see Security Group Configurations.
- Python has been installed on the ECS. The recommended version is 2.7.6 or later.
- You have obtained the python-binary-memcached-x.y.z.zip dependency package.
x.y.z indicates the version of the dependency package. The latest version is recommended.
Connecting to Memcached on the Python-binary-memcached
- Log in to the DCS console.
- Click in the upper left corner of the management console and select the region where your instance is located.
- In the navigation pane, choose Cache Manager.
- On the Cache Manager page, click the name of the DCS Memcached instance you want to access. Obtain the IP address and port number of the instance.
- Upload the obtained dependency package (for example, the python-binary-memcached-x.y.z.zip package) to the created ECS.
- Log in to the ECS.
- Run the following commands to install the dependency package:
unzip -xzvf python-binary-memcached-x.y.z.zip
cd python-binary-memcached-x.y.z
python setup.py install
If an error is reported during the installation, use the apt or yum installation method. For example, to install the dependency package by using the apt method, run the following commands:
apt install python-pip;
pip install python-binary-memcached;
- Create a Python file named dcs_test.py, copy the following Python code to the file, and modify the code.
- Example code for the password mode
Change ip:port to the IP address and port number obtained in 4. Set userName and password respectively to the username and password of the Memcached instance.
import bmemcached client = bmemcached.Client(('ip:port'), 'userName', 'password') print "set('key', 'hello world!')" print client.set('key', 'hello world!') print "get('key')" print client.get('key')
- Example code for the password-free mode
Change ip:port to the IP address and port number obtained in 4.
import bmemcached client = bmemcached.Client('ip:port') print "set('key', 'hello world!')" print client.set('key', 'hello world!') print "get('key')" print client.get('key')
- Example code for the password mode
- Run the dcs_test.py file. The following result is displayed.
# python test.py set('key', 'hello world!') True get('key') hello world!
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot