summaryrefslogtreecommitdiff
path: root/docs/examples/connection_example.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/connection_example.ipynb')
-rw-r--r--docs/examples/connection_example.ipynb254
1 files changed, 254 insertions, 0 deletions
diff --git a/docs/examples/connection_example.ipynb b/docs/examples/connection_example.ipynb
new file mode 100644
index 0000000..af5193e
--- /dev/null
+++ b/docs/examples/connection_example.ipynb
@@ -0,0 +1,254 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Connection Examples"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import redis"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Connecting to a default Redis instance, running locally."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "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": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "decode_connection = redis.Redis(decode_responses=True)\n",
+ "connection.ping()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Connecting to a redis instance, specifying a host and port with credentials."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "user_connection = redis.Redis(host='localhost', port=6380, username='dvora', password='redis', decode_responses=True)\n",
+ "user_connection.ping()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Connecting to a Redis instance via SSL."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 5,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "ssl_connection = redis.Redis(host='localhost', port=6666, ssl=True, ssl_cert_reqs=\"none\")\n",
+ "ssl_connection.ping()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Connecting to a Redis instance via SSL, while specifying a self-signed SSL certificate."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "import os\n",
+ "\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",
+ "ssl_cert_conn = redis.Redis(\n",
+ " host=\"localhost\",\n",
+ " port=6666,\n",
+ " ssl=True,\n",
+ " ssl_certfile=ssl_certfile,\n",
+ " ssl_keyfile=ssl_keyfile,\n",
+ " ssl_cert_reqs=\"required\",\n",
+ " ssl_ca_certs=ssl_ca_certs,\n",
+ ")\n",
+ "ssl_cert_conn.ping()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "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",
+ "Three URL schemes are supported:\n",
+ "\n",
+ "- `redis://` creates a TCP socket connection. <https://www.iana.org/assignments/uri-schemes/prov/redis>\n",
+ "- `rediss://` creates a SSL wrapped TCP socket connection. <https://www.iana.org/assignments/uri-schemes/prov/rediss>\n",
+ "- ``unix://``: creates a Unix Domain Socket connection.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "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\")"
+ ]
+ }
+ ],
+ "metadata": {
+ "interpreter": {
+ "hash": "d45c99ba0feda92868abafa8257cbb4709c97f1a0b5dc62bbeebdf89d4fad7fe"
+ },
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.8.12"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}