How to connect to memcachedb and use API

267 Views Asked by At

I have installed memcachedb according to the Memcachedb: The complete guide, and I am able to set and get the key,values using telnet as explained in the guide.

What I really want to do is to set and get the key, value pairs from a python script.

I have the memcachedb running on the Ubuntu machine by following command:

sudo ./memcachedb -vv -u root -H ~/mcDB/ -N

I read and found out that libmemcached python client can be used to communicate with memcachedb. So, I am using the following test script

import memcache

client=memcache.Client([('localhost',21201)]) # port for memcachedb

print "return value  " + str(client.set("key","value"))

print "get result  " + str(client.get("key"))

But it gives the following output:

return value 0

get result None

I have also tried replacing localhost with 127.0.0.1, does not work either.

In fact, there is no output by memcachedb (-vv option) on running the python script while there is when I use telnet to set and get.

So how can I connect to memcachedb and execute commands through python (get and set)?

1

There are 1 best solutions below

0
On BEST ANSWER

So instead of python-memcached, I tried pylibmc and now that script is working. There is probably some problem with python-memcached.

Updated script looks as follows:

import pylibmc

client=pylibmc.Client(["127.0.0.1:21201"]) # port for memcachedb

print "return value  " + str(client.set("key","value"))

print "get result  " + str(client.get("key"))