使用 memcached.sock 路径连接到 python-memcached

Connect to python-memcached using memcached.sock path

如何使用 memcached.sock 的路径连接到 Python-memcached? (Python 2.7)

我的主机 (Webfaction) 上预装了 Memcached。我已经启动它并验证它是运行。我还验证了 memcached.sock 在我的主目录中。文档说:

"Once your Memcached instance is up and running, you can access it by the path of the socket file (~/memcached.sock) with the software or library which uses memcached."

我试过这个:

import memcache
mc = memcache.Client(['127.0.0.1:11211'], debug=1)
mc.set("some_key", "Some value")

但我在 mc.set 上收到错误消息:

Connection refused.  Marking dead.

我也试过了

mc = memcache.Client('~/memcached.sock', debug=1)

那么mc.set上的错误就是

Name or service not known.  Marking dead.

我通过这样做让它工作:

设置:

memcached -d -s /tmp/memcached.sock

代码:

import memcache
mc = memcache.Client(['unix:/tmp/memcached.sock'], debug=0)
mc.set('hello', 'world')
print(mc.get('hello'))

完整测试:

docker run --rm -t python:3.6 bash -c "$(cat << 'EOF'
# setup
apt-get update && \
apt-get install -y memcached && \

# start memcached
memcached -u nobody -d -s /tmp/memcached.sock && \

# install the requirements
pip install python-memcached && \

# run the code
python <(cat << FOE
import memcache
mc = memcache.Client(['unix:/tmp/memcached.sock'], debug=0)
mc.set('hello', 'world')
print(mc.get('hello'))
FOE
)

EOF
)"

...剧透,打印出来"world"