Utility Methods API for PubNub Python SDK
Python version support
Python SDK versions 5.0.0 and higher no longer support Python v2.7 and the Twisted and Tornado frameworks. If you require support for any of these, use SDK version 4.8.1.
Note that PubNub will stop supporting versions of Python lower than 3.7 by the end of 2021.
The methods on this page are utility methods that don't fit into other categories.
Time
Description
This function will return a 17 digit precision Unix epoch.
The timetoken is constructed using the following algorithm:
timetoken = (Unix epoch time in seconds) * 10000000
Example of creating a timetoken for a specific time and date
08/19/2013 @ 9:20pm in UTC = 1376961606
timetoken = 1376961606 * 10000000
timetoken = 13769616060000000
Method(s)
To fetch Time
you can use the following method(s) in Python SDK:
pubnub.time()
Basic Usage
envelope = pubnub.time().sync()
Returns
The time()
operation returns a PNTimeResponse
which contains the following operations:
Method | Type | Description |
---|---|---|
int | Int | Returns an int representation of current timetoken. |
str | String | Returns a str representation of current timetoken. |
date_time | Date | Returns a date representation of current timetoken. |
Do not confuse with timestamp()
method, which is a shortcut to int(time.time())
Get Subscribed Channels
Description
Returns all the subscribed channels in a list
.
Method(s)
To Get Subscribed Channels
you can use the following method(s) in the Python SDK:
pubnub.get_subscribed_channels()
Basic Usage
channels = pubnub.get_subscribed_channels()
Returns
List
["my_ch1", "my_ch2"]
Get Subscribed Channel Groups
Description
Returns all the subscribed channel groups in a list
.
Method(s)
To Get Subscribe Channel Groups
you can use the following method(s) in the Python SDK:
pubnub.get_subscribed_channel_groups()
Basic Usage
Get Subscribed Channel Groups :
channels = pubnub.get_subscribed_channel_groups()
Returns
List
["my_group1", "my_group2"]
Encrypt
Description
This function allows to encrypt
the data.
Method(s)
To encrypt
the data you can use the following method(s) in Python SDK.
pubnub.encrypt(cipher_key, file)
Parameter | Type | Required | Description |
---|---|---|---|
file | bytes | Yes | The file to encrypt . |
cipher_key | String | Optional | Cipher key used for encryption. If it's not provided, the cipher key from config is used. |
Basic Usage
payload_to_encrypt = b'knights_who_say_ni'
encrypted_payload = pubnub.encrypt("test_cipher_key", payload_to_encrypt)
Returns
Bytes object with encrypted data.
Decrypt
Description
This function allows to decrypt
the data.
Method(s)
To decrypt
the data you can use the following method(s) in Python SDK.
pubnub.decrypt(cipher_key, file)
Parameter | Type | Required | Description |
---|---|---|---|
file | bytes | Yes | The file to decrypt . |
cipher_key | String | Optional | Cipher key used for encryption. If it's not provided, the cipher key from config is used. |
Basic Usage
decrypted_payload = pubnub.decrypt(cipher_key, payload_to_decrypt_in_bytes)
Returns
Bytes object with decrypted data.