A very minimal Python Redis client library (not only) for MicroPython
Project description
Overview
PicoRedis is a very minimal Redis client (not only) for MicroPython.
What is does
Support the REdis Serialization Protocol (RESP).
Connect to a Redis server via TCP.
Send Redis commands and receive and parse the response in a simple, blocking fashion.
Support MicroPython (unix and bare-metal ports with usocket and uselect module), CPython and PyPy (3.4+, 2.7+ untested).
What it does not
Parse the response beyond de-serialization of the basic RESP types (simple string, error, bulk string, integer and array).
Decode response byte strings, except error messages.
Support the subscribe / publish protocol.
Support SSL / TLS (yet).
Async I/O.
Usage
>>> from picoredis import Redis
>>> redis = Redis() # server defaults to 127.0.0.1 port 6379
>>> redis.do_cmd('PING', 'Hello World!')
b'Hello World!'
Instead of using the do_cmd method, Redis instances can be called directly:
>>> redis('SET', 'foo', 'bar')
b'OK'
>>> redis('GET', 'foo')
b'bar' # string responses are always byte strings
Or you can call arbitrary methods on the Redis instance, 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 of only letters, except connect, close, debug (and do_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 a RedisError exception will be raised:
>>> redis.bogus('spam!')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "picoredis.py", line 72, in <lambda>
File "picoredis.py", line 66, in do_cmd
File "picoredis.py", line 82, in _read_response
RedisError: ('ERR', "unknown command 'bogus'")
Connection
When you create a Redis instance, it immediatly tries to open a connecting to the Redis server. The default host and port are 127.0.0.1 and 6379 respectively.
You can set the host name or IP address and port number of the Redis server to connect with the host and port keyword arguments:
>>> redis = Redis('192.168.1.100')
>>> redis = Redis(port=6380)
>>> redis = Redis('192.168.1.100', 6380)
>>> redis = Redis(server='192.168.1.100')
>>> redis = Redis(server='192.168.1.100', port=6380)
You can set the TCP socket timeout with the timeout keyword 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, a RedisTimeout exception is raised.
To close the connection to the server, use the close() method:
>>> redis.close()
>>> redis.ping()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "picoredis.py", line 89, in <lambda>
File "picoredis.py", line 75, in do_cmd
RedisError: Not connected: use 'connect()' to connect to Redis server.
To open a new connection again, use the connect method. 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 Output
To turn on printing of raw messages sent to and received from the Redis server pass debug=True when creating the instance or set its debug attribute to True:
>>> 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']
Tips
If 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:
>>> class MyRedis(Redis):
... def command_list(self):
... return sorted([cmd[0].decode('utf-8')
... for cmd in self.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 a MemoryError, when you run it on a memory-constrained device like an ESP8266-based board.
Installation
On CPython and PyPy use pip to install as usual:
$ pip install picoredis
On the MicroPython unix port, use the upip:
$ micropyton -m upip install picoredis
On MicroPython base-metal ports (esp8266, stm32, wipy, etc.), just download the picoredis.py file from the repository and upload it to the flash storage of your MicroPython board, e.g. using ampy:
$ curl -O https://raw.githubusercontent.com/SpotlightKid/micropython-stm-lib/master/picoredis/picoredis/picoredis.py $ ampy -p /dev/ttyUSB0 put picoredis.py
You can also compile the picoredis.py module with mpy-cross and use the resulting picoredis.mpy file 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.mpy
License
PicoRedis was written and is copyrighted by Christopher Arndt, 2017.
It is distributed under the terms of the MIT license, PicoRedis is free and open source software.
Acknowledgements
Some inspiration and code ideas were taken from these projects:
micropython-redis by Dwight Hubbard
redis_protocol by Young King
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file picoredis-0.1.0.tar.gz
.
File metadata
- Download URL: picoredis-0.1.0.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c5f908c5d304c051400c0c6c1a7cc2922b3005326e1ffaa0b6d29ba0495d240 |
|
MD5 | 9e81728a89403a035ea9c9dfe0804cb7 |
|
BLAKE2b-256 | 69234548bc790b2cfb005a75a084c92d9284deadbe85fc1f6398e251fac831e3 |
File details
Details for the file picoredis-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: picoredis-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 876521fc7c58ef807cb7803eaeb09477c04e9cd917a2675f9c0c50e7bd790c58 |
|
MD5 | 1bca9352a060a22abf06ea1c1a52737a |
|
BLAKE2b-256 | ffed6664b6052fb117adb8b7e4ea3201056bcf4b7b41dab4c421e211f52a2793 |