Skip to main content

WebPush library for python

Project description

WebPush-Py

PyPI - Version

Simple library for working with WebPush

Usage

Installation

pip install webpush

Basic Usage

import requests
from webpush import WebPush, WebPushSubscription
from pathlib import Path

wp = WebPush(
    private_key=Path("./private_key.pem"),
    public_key=Path("./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

from pathlib import Path

import aiohttp
from fastapi import FastAPI
from fastapi.responses import JSONResponse

from webpush import WebPush, WebPushSubscription

app = FastAPI()

wp = WebPush(
    public_key=Path("./public_key.pem"),
    private_key=Path("./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"})

More examples

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

webpush-1.0.6.tar.gz (38.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

webpush-1.0.6-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file webpush-1.0.6.tar.gz.

File metadata

  • Download URL: webpush-1.0.6.tar.gz
  • Upload date:
  • Size: 38.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.19

File hashes

Hashes for webpush-1.0.6.tar.gz
Algorithm Hash digest
SHA256 62cd7cb7d09d81d8759635913ff4390001b31ae831b8351b6818767a5ef4659d
MD5 463203dc6ae3f020c2bf4a712d0f83b2
BLAKE2b-256 b3a51ff5b2f17c9fe52e697cb153a7a3be32386c6d29aa893183ac5c1d6f0de5

See more details on using hashes here.

File details

Details for the file webpush-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: webpush-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.19

File hashes

Hashes for webpush-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 88103d23ccb4cd2474ca8de538277c239ada7d70ef1e70785ca7c568d9c49b0e
MD5 18ba4620d593e639193dfe448d69824a
BLAKE2b-256 47d4356b5d340d233f005d1ec43b30ee512c3096f88b12a3130e620af134363b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page