Help Center > > User Guide> Accessing a DCS Memcached Instance> Using Python to Access an Instance

Using Python to Access an Instance

Updated at: Mar 17, 2020 GMT+08:00

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 in different VPCs, establish a VPC peering connection to achieve network connectivity between the ECS and DCS instance. For details, see Do DCS Instances Support Cross-VPC Access?

    If different security groups have been configured for the ECS and DCS instance, set security group rules to achieve network connectivity between the ECS and DCS instance. For details, see How to Select and Configure a Security Group?

  • 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.

Procedure

  1. Log in to the DCS console.
  2. Click in the upper left corner of the management console and select a region and a project.

    Select the same region as your application service.

  3. In the navigation pane, choose Cache Manager.
  4. On the Cache Manager page, click the name of the DCS Memcached instance you want to access. Obtain the IP address or domain name and port number of the instance.
  5. Upload the obtained dependency package (for example, the python-binary-memcached-x.y.z.zip package) to the created ECS.
  6. Log in to the ECS.
  7. 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;

  8. 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 address or domain name:port to the IP address or domain name 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 or domain name: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 address or domain name:port to the IP address and port number obtained in 4.

      import bmemcached
      client = bmemcached.Client('ip or domain name:port')
      print "set('key', 'hello world!')"
      print client.set('key', 'hello world!')
      print "get('key')"
      print client.get('key')

  9. Run the dcs_test.py file. The following result is displayed.

    # python test.py 
    set('key', 'hello world!')
    True
    get('key')
    hello world!

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel