Package: picoredis
OverviewPicoRedisis a very minimal Redis client (not only) for MicroPython.What is doesSupport theREdisSerializationProtocol
(RESP).Connect to a Redis server via TCP.SendRedis commandsand receive and
parse the response in a simple, blocking fashion.Support MicroPython (unix and bare-metal ports withusocketanduselectmodule), CPython and PyPy (3.4+, 2.7+ untested).What it does notParse the response beyond de-serialization of the basic RESP types
(simple string,error,bulk string,integerandarray).Decode response byte strings, except error messages.Support the subscribe / publish protocol.Support SSL / TLS (yet).Async I/O.Usage>>>frompicoredisimportRedis>>>redis=Redis()# server defaults to 127.0.0.1 port 6379>>>redis.do_cmd('PING','Hello World!')b'Hello World!'Instead of using thedo_cmdmethod,Redisinstances can be
called directly:>>>redis('SET','foo','bar')b'OK'>>>redis('GET','foo')b'bar'# string responses are always byte stringsOr you can call arbitrary methods on theRedisinstance, and the
method name will be used as the Redis command:>>>redis.hset('myhash','key1',42)1>>>redis.hkeys('myhash')[b'key1']You can use any method name consisting ofonlyletters, exceptconnect,close,debug(anddo_cmd), which are already
used as instance attribute or method names. If the name does not
correspond to a valid Redis command, the server will return an error and
aRedisErrorexception will be raised:>>>redis.bogus('spam!')Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"picoredis.py",line72,in<lambda>File"picoredis.py",line66,indo_cmdFile"picoredis.py",line82,in_read_responseRedisError:('ERR',"unknown command 'bogus'")ConnectionWhen you create aRedisinstance, it immediatly tries to open a
connecting to the Redis server. The default host and port are127.0.0.1and6379respectively.You can set the host name or IP address and port number of the Redis
server to connect with thehostandportkeyword arguments:>>>redis=Redis('192.168.1.100')>>>redis=Redis(port=6380)>>>redis=Redis('192.168.1.100',6380)>>>redis=Redis(host='192.168.1.100')>>>redis=Redis(host='192.168.1.100',port=6380)You can set the TCP socket timeout with thetimeoutkeyword argument
in milliseconds (default 3000):>>>redis=Redis(timeout=10000)If a response is read from the server and the server doesn’t return any
data within the timeout, aRedisTimeoutexception is raised.To close the connection to the server, use theclose()method:>>>redis.close()>>>redis.ping()Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"picoredis.py",line89,in<lambda>File"picoredis.py",line75,indo_cmdRedisError:Notconnected:use'connect()'toconnecttoRedisserver.To open a new connection again, use theconnectmethod. You can pass
a different host name and / or port number and they will overwrite the
ones given when the instance was created:>>>redis.connect('redis.myserver.com')>>>redis._host'redis.myserver.com'Debug OutputTo turn on printing of raw messages sent to and received from the Redis
server passdebug=Truewhen creating the instance or set itsdebugattribute toTrue:>>>redis=Redis(debug=True)>>>redis.hkeys('myhash')SEND:'*2\r\n$5\r\nhkeys\r\n$6\r\nmyhash\r\n'RECV:b'*1\r\n'RECV:b'$4\r\n'RECV:b'key1\r\n'[b'key1']TipsIf you need to further parse the response to a Redis command regularly,
just add a wrapper method in a sub-class. For example, here is how to
get the list of commands supported by the Redis server as a list of
strings:>>>classMyRedis(Redis):...defcommand_list(self):...returnsorted([cmd[0].decode('utf-8')...forcmdinself.do_cmd('command')])>>>redis=MyRedis()>>>redis.command_list()['append','asking','auth','bgrewriteaof','bgsave','bitcount','bitfield',...,'zunionstore']Warning:The response to this command sent be the Redis server will
be fairly big and probably cause aMemoryError, when you run it on a
memory-constrained device like an ESP8266-based board.InstallationOn CPython and PyPy usepipto install as usual:$ pip install picoredisOn MicroPython, just download thepicoredis.pyfile from the repository and, for the unix port, put it into your
MICROPYPATH directory (normally~/.micropython/lib), or for
base-metal ports (esp8266,stm32,wipy, etc.) upload it to the
flash storage of your MicroPython board, for example usingampy:$ curl -O https://raw.githubusercontent.com/SpotlightKid/picoredis/master/picoredis/picoredis.py
$ ampy -p /dev/ttyUSB0 put picoredis.pyYou can also compile thepicoredis.pymodule withmpy-crossand use the resultingpicoredis.mpyfile as a drop-in replacement
for the pure Python version. This will save you a good bit of memory on
your MicroPython board, because the byte-code compilation step, that
normally happens when you import the module, can be skipped:$ mpy-cross picoredis.py
$ ampy -p /dev/ttyUSB0 put picoredis.mpyLicensePicoRediswas written and is copyrighted by Christopher Arndt, 2017.It is distributed under the terms of theMIT
license,PicoRedisis free
and open source software.AcknowledgementsSome inspiration and code ideas were taken from these projects:micropython-redisby Dwight Hubbardredis_protocolby
Young King