Quickly interact with PubNub's Python SDK.
Project description
pubnub-python-tools
Quickly interact with PubNub using the Python SDK.
Pre-requisites
For MacOS use homebrew
to install packages or the following PubNub Ansible Role Python SDK that sets everything for you.
- python3
- python-tk
Install
To install pubnub-python-tools
:
Install using pip
This is only uploaded in the testing repository version of
pypi
as this is not yet officially released. Check it out: https://test.pypi.org/project/pubnub-python-tools/
To install the latest managed packaged version:
$ python -m pip install -i https://test.pypi.org/simple/ pubnub-python-tools
Automatic build and install
It is recommended to setup a virtual environment.
To install locally the latest git version:
$ git clone https://github.com/sergio-munoz/pubnub-python-tools
$ ./scripts/build_install.sh
Set script permissions with
chmod +x
Manually build and install
Setup a new python virtual environment and install packages in requirements_build.txt
:
$ python3 -m venv build_venv
$ source build_venv/bin/activate
(build_venv) $ pip install -r build_requirements.txt
(build_venv) $ hatch build
(build_venv) $ pip install dist/pubnub_python_tools-${VERSION}.tar.gz
Install into a Jupyter Notebook
To install the latest testing repository version into the Jupyter Notebook kernel:
import sys
!{sys.executable} -m pip install -U pubnub
!{sys.executable} -m pip install -U --index-url https://test.pypi.org/simple/ --no-deps pubnub-python-tools
PubNub Auth Settings
Always set the UUID (USER_ID) to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing.
To run pubnub
commands you need to setup your PubNub credentials using any of the following ways:
Automatically
Run the setup.sh
script with your PubNub subscribe key, publish key and user_id:
$ ./scripts/install.sh $PN_SUBSCRIBE_KEY $PN_PUBLISH_KEY $PN_USER_ID
Using an .env file
Requires python-dotenv. Install using:
pip install python-dotenv
If you can't see your
.env
file try using:ls -la
Create an .env
file to avoid typing your credentials each time.
Copy the env file sample env
file into your own .env
(watch the dot '.'
) file and replace with your PubNub keys.
$ cp env .env
A minimal .env
file to work properly needs at least your PubNub's subscribe key
, publish key
and a chosen user id
:
$ cat .env
PN_SUBSCRIBE_KEY=sub-xxx-xxx
PN_PUBLISH_KEY=pub-xxx-xxx
PN_USER_ID=UUID
Manually via CLI
This has the highest precedence
Manually set cli variables before each command:
-sk $PN_SUBSCRIBE_KEY -pk $PN_PUBLISH_KEY -u $PN_USER_ID
CLI Args:
-sk
,--subscribe-key
- PubNub subscribe key.-pk
,--publish-key
- PubNub publish key.-u
,--user-id
- User ID (UUID).
Overriding Global variables
This has the lowest precedence
NOTE: It is not recommended to hard-code your credentials due to security purposes.
Override the global variables manually in file pubnub_python_tools/config/module_config.py
:
# OVERRIDE GLOBAL VARIABLES
SUBSCRIBE_KEY = "pub-xxx-xxx"
PUBLISH_KEY = "sub-xxx-xxx"
USER_ID = "UUID"
Sample Use
Run PubNub using the script: run_pn.py
.
$ python ./scripts/run_pn.py -s "Space01" -p "Space01" -m "Hello from MySpace01"
Or if installed, run PubNub using the command: pubnub-python-tools
.
$ pubnub-python-tools -s "Space01" -p "Space01" -m "Hello from MySpace01"
Get help using the --help
flag.
$ python ./scripts/run_pn.py --help
$ pubnub-python-tools --help
CLI Usage Reference
Remember to always set up your UUID
USER_ID
with-u
.
Subscribe
Subscribe to a channel forever.
python run_pn.py -s "Space"
CLI Args:
-sk
,--subscribe-key
- PubNub subscribe key (or set in.env
)-s
,--subscribe
- PubNub subscribe channel name
Publish
Publish a message to a channel.
python run_pn.py -p "Space" -m "payload"
CLI Args:
-sk
,--subscribe-key
- PubNub subscribe key (or set in.env
)-pk
,--publish-key
- PubNub publish key (or set in.env
)-p
,--publish
- PubNub publish channel name-m
,--message
- Message to publish
For some reason the pubnub python sdk fails to publish when instantiated without a subscribe key, so pass it to avoid issues.
Presence
Subscribe to a channel with Presence forever.
python run_pn.py -s "Space" -pres
CLI Args:
-sk
,--subscribe-key
- PubNub subscribe key (or set in.env
)-s
,--subscribe
- PubNub subscribe channel name-pres
,--presence
- Presence flag
HereNow
Call Here Now
on a channel.
python run_pn.py -here "Space"
CLI Args:
-sk
,--subscribe-key
- PubNub subscribe key (or set in.env
)-here
,--here-how
- Here now on a channel name
For advanced HereNow
topics see: cache busting information. NOTE: TBD SOON
Unsubscribe
Send a leave event to a channel subscribed with Presence.
python run_pn.py -us "Space"
CLI Args:
-sk
,--subscribe-key
- PubNub subscribe key (or set in.env
)-us
,--unsubscribe
- PubNub channel name to unsubscribe from
Use Cases
Subscribe and Publish
Because subscribe and publish happen so fast the subscribe might not be listening when the publish was made and thus it will not be shown, but it will be published, if other devices are listening, they will reflect the changes. This is classic pub/sub behavior.
Open one terminal and run:
(venv) $ python run_pn.py -s "Space" -p "Space" -m "payload 1"
Open another terminal and run that command modified to show that the user is still subscribed the channel:
(venv) $ python run_pn.py -s "Space" -p "Space" -m "payload 2"
Dig into the logger/main_log.log
to see more information.
Tests
Unit Tests
To all run tests use unittest
:
(venv) $ python -m unittest tests/test_*
----------------------------------------------------------------------
Ran 20 tests in 30.680s
OK
Coverage
(venv) $ python -m coverage run -m unittest discover
(venv) $ python -m coverage report -i
Name Stmts Miss Cover
------------------------------------------------------------------------------
src/pubnub_python_tools/app/device_manager.py 74 16 78%
src/pubnub_python_tools/app/main_app.py 46 35 24%
src/pubnub_python_tools/app/pubnub_config.py 16 0 100%
src/pubnub_python_tools/app/pubnub_handle_disconnects.py 29 20 31%
src/pubnub_python_tools/app/pubnub_here_now_callback.py 45 9 80%
src/pubnub_python_tools/app/pubnub_listener.py 133 73 45%
src/pubnub_python_tools/app/pubnub_manager.py 89 33 63%
src/pubnub_python_tools/app/pubnub_manager_asyncio.py 48 11 77%
src/pubnub_python_tools/app/pubnub_on_request.py 3 0 100%
src/pubnub_python_tools/app/pubnub_publish.py 15 11 27%
src/pubnub_python_tools/cli/v1.py 18 0 100%
src/pubnub_python_tools/config/module_config.py 20 3 85%
src/pubnub_python_tools/logger/logging_config.py 32 9 72%
tests/__init__.py 4 0 100%
tests/test_device_manager.py 67 0 100%
tests/test_main_app.py 9 0 100%
tests/test_pubnub_manager.py 79 0 100%
tests/test_pubnub_manager_asyncio.py 102 10 90%
tests/test_request_function.py 15 0 100%
tests/test_v1.py 32 0 100%
------------------------------------------------------------------------------
TOTAL 876 230 74%
6 empty files skipped.
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
Hashes for pubnub_python_tools-1.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a4e0e5f6355518c5c6da41bf85277d47dcbe318b4c1f494bd8b9762f4cb1358 |
|
MD5 | 1c06a1f4a752975442326e22e5072d27 |
|
BLAKE2b-256 | 558eeab1d58b0df57803a97f4ecc056c02d2a6f176b336ea90c9e6fbf680b52c |
Hashes for pubnub_python_tools-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2744c7e9df9190005323ae162c9d349c8a8ab0e51f6ec3e0e552b23322f91383 |
|
MD5 | 71d56c93c352fe7deb7cee1c3093aa59 |
|
BLAKE2b-256 | a173ce399169e76207e2f901633608e6e3729880a89b62da01a61196ed5bf8a3 |