WebPush library for python
Project description
WebPush-Py
Simple library for working with WebPush in python
Usage
Installation
pip install webpush
Basic Usage
import requests
from webpush import WebPush, WebPushSubscription
wp = WebPush(private_key="./private_key.pem", public_key="./public_key.pem")
# example subscription info
subscription = WebPushSubscription.model_validate({
"endpoint": "https://fcm.googleapis.com/fcm/send/...",
"keys": {
"auth": "...",
"p256dh": "..."
}
})
message = wp.get(message='Hello, world!', subscription=subscription)
requests.post(subscription.endpoint, data=message.encrypted, headers=message.headers)
Generate VAPID keys and get applicationServerKey:
vapid-gen
Private key saved in public_key.pem
and public key saved in public_key.pem
.
Application Server Key saved in applicationServerKey
simple usage with fastapi
import aiohttp
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from webpush import WebPush, WebPushSubscription
app = FastAPI()
wp = WebPush(
public_key="./public_key.pem",
private_key="./private_key.pem",
subscriber="admin@mail.com",
)
@app.get("/notification/key")
async def get_public_key() -> JSONResponse:
application_server_key = "<generated from vapid-gen>"
return JSONResponse(content={"public_key": application_server_key})
@app.post("/notification/subscribe")
async def subscribe_user(subscription: WebPushSubscription) -> JSONResponse:
message = wp.get(message="Hello, world", subscription=subscription)
async with aiohttp.ClientSession() as session:
await session.post(
url=str(subscription.endpoint),
data=message.encrypted,
headers=message.headers,
)
return JSONResponse(content={"status": "ok"})
FAQ
- Why do I need another library?
The current python libraries that work with Web Push have been written for a very long time, so they do not support typing, try to support outdated encryption algorithms and pull a lot of deprecated dependencies.
- Why is only
aes128gcm
supported?
According to the RFC8192, this is the recommended format. At the moment, all modern systems support this encryption.
- Will there be support for other encryption modes?
New, yes, but there are no old ones, for example aesgcm
- Who is this library for?
You need type support, you're writing a modern backend, minimum number of dependencies.
And last one, if you have ideas for improvements, bug fixes, feel free to contribute.
Change log
- 1.0.0 - initial release
Credits
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 webpush-1.0.2.tar.gz
.
File metadata
- Download URL: webpush-1.0.2.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.1 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 890e004b21f4782a5feff2e477db974cf838dfa029cfeab1e600ad7f7d2ca284 |
|
MD5 | 516300f08bf8685978e25e5c0354454d |
|
BLAKE2b-256 | 3e43a24b87dad97baf26df60f6cc40d51681b447b5eb4b2fd19e8d920404e1e0 |
File details
Details for the file webpush-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: webpush-1.0.2-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.1 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 658496ad9d348c0482a326282bbceef7ce9de8a2b41f05f8a9f059d2e6d3b667 |
|
MD5 | b3a8a207211807d0950f0c2b3bfbb7b0 |
|
BLAKE2b-256 | bc356f63c7dcadce9e9933158752c995109394d7b5f76fde3dbcf82899b2d56d |