diff options
Diffstat (limited to 'docs/examples/connection_example.ipynb')
-rw-r--r-- | docs/examples/connection_example.ipynb | 172 |
1 files changed, 121 insertions, 51 deletions
diff --git a/docs/examples/connection_example.ipynb b/docs/examples/connection_example.ipynb index b19edb3..af5193e 100644 --- a/docs/examples/connection_example.ipynb +++ b/docs/examples/connection_example.ipynb @@ -8,6 +8,15 @@ ] }, { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import redis" + ] + }, + { "cell_type": "markdown", "metadata": {}, "source": [ @@ -16,21 +25,51 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "connection = redis.Redis()\n", + "connection.ping()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### by default Redis return binary responses, to decode them use decode_responses=True" + ] + }, + { + "cell_type": "code", + "execution_count": 3, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "import redis\n", - "r = redis.Redis()\n", - "print(r.ping())" + "decode_connection = redis.Redis(decode_responses=True)\n", + "connection.ping()" ] }, { @@ -42,21 +81,23 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 4, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "import redis\n", - "r = redis.Redis(host='localhost', port=6380, username='dvora', password='redis')\n", - "print(r.ping())" + "user_connection = redis.Redis(host='localhost', port=6380, username='dvora', password='redis', decode_responses=True)\n", + "user_connection.ping()" ] }, { @@ -68,21 +109,23 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 5, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "import redis\n", - "r = redis.Redis(host='localhost', port=6666, ssl=True, ssl_cert_reqs=\"none\")\n", - "print(r.ping())" + "ssl_connection = redis.Redis(host='localhost', port=6666, ssl=True, ssl_cert_reqs=\"none\")\n", + "ssl_connection.ping()" ] }, { @@ -94,34 +137,39 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 6, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "import os\n", - "import redis\n", "\n", - "ROOT = os.path.join(os.getcwd(), \"..\")\n", + "ROOT = os.path.join(os.getcwd(), \"..\", \"..\")\n", "CERT_DIR = os.path.abspath(os.path.join(ROOT, \"docker\", \"stunnel\", \"keys\"))\n", + "ssl_certfile=os.path.join(CERT_DIR, \"server-cert.pem\")\n", + "ssl_keyfile=os.path.join(CERT_DIR, \"server-key.pem\")\n", + "ssl_ca_certs=os.path.join(CERT_DIR, \"server-cert.pem\")\n", "\n", - "r = redis.Redis(\n", + "ssl_cert_conn = redis.Redis(\n", " host=\"localhost\",\n", " port=6666,\n", " ssl=True,\n", - " ssl_certfile=os.path.join(CERT_DIR, \"server-cert.pem\"),\n", - " ssl_keyfile=os.path.join(CERT_DIR, \"server-key.pem\"),\n", + " ssl_certfile=ssl_certfile,\n", + " ssl_keyfile=ssl_keyfile,\n", " ssl_cert_reqs=\"required\",\n", - " ssl_ca_certs=os.path.join(CERT_DIR, \"server-cert.pem\"),\n", + " ssl_ca_certs=ssl_ca_certs,\n", ")\n", - "print(r.ping())" + "ssl_cert_conn.ping()" ] }, { @@ -129,7 +177,8 @@ "metadata": {}, "source": [ "## Connecting to Redis instances by specifying a URL scheme.\n", - "Parameters are passed to the following schems, as parameters to the url scheme.\n\n", + "Parameters are passed to the following schems, as parameters to the url scheme.\n", + "\n", "Three URL schemes are supported:\n", "\n", "- `redis://` creates a TCP socket connection. <https://www.iana.org/assignments/uri-schemes/prov/redis>\n", @@ -139,21 +188,42 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 7, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "import redis\n", - "r = redis.from_url(\"rediss://localhost:6666?ssl_cert_reqs=none\")\n", - "print(r.ping())" + "url_connection = redis.from_url(\"rediss://localhost:6666?ssl_cert_reqs=none&decode_responses=True&health_check_interval=2\")\n", + "\n", + "url_connection.ping()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Connecting to a Sentinel instance" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from redis.sentinel import Sentinel\n", + "sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)\n", + "sentinel.discover_master(\"redis-py-test\")" ] } ], @@ -176,7 +246,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.9" + "version": "3.8.12" } }, "nbformat": 4, |