Skip to main content

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

  • 0.1.2 - small changes in internal components
  • 0.1.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.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

webpush-1.0.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: webpush-1.0.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.5.6-arch2-1

File hashes

Hashes for webpush-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8853fa8034f9d10c901c45e7b6d4b691abf72e6a62af3ab48aa74d685625b0e3
MD5 89be94c4a3caa6d3561415610fb24541
BLAKE2b-256 988f4cec2b8dd1237dae56a0d71f02bdc281d56cec9a24660080c343bafc53ec

See more details on using hashes here.

File details

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

File metadata

  • Download URL: webpush-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.5.6-arch2-1

File hashes

Hashes for webpush-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3131c1620ca63f9bbb7e4effac0197abb231f9189ccd7d79750e0920ac09e64
MD5 5cb6de40758606de6a1549b135fb288c
BLAKE2b-256 07b4c4da428a974483e9573ad21ab3760f5e43ccf84d342d2dae1315ec52a7d8

See more details on using hashes here.

Supported by

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