@@ -10,12 +10,12 @@ available on `github <https://github.com/mozilla-services/pywebpush>`__.
10
10
Installation
11
11
------------
12
12
13
- You' ll need to run ``python virtualenv ``. Then
13
+ You’ ll need to run ``python virtualenv ``. Then
14
14
15
15
::
16
16
17
- bin/pip install -r requirements.txt
18
- bin/python setup.py develop
17
+ bin/pip install -r requirements.txt
18
+ bin/python setup.py develop
19
19
20
20
Usage
21
21
-----
@@ -31,94 +31,94 @@ As illustration, a ``subscription_info`` object may look like:
31
31
32
32
.. code :: json
33
33
34
- {"endpoint" : " https://updates.push.services.mozilla.com/push/v1/gAA..." , "keys" : {"auth" : " k8J..." , "p256dh" : " BOr..." }}
34
+ {"endpoint" : " https://updates.push.services.mozilla.com/push/v1/gAA..." , "keys" : {"auth" : " k8J..." , "p256dh" : " BOr..." }}
35
35
36
36
How you send the PushSubscription data to your backend, store it
37
- referenced to the user who requested it, and recall it when there' s a
37
+ referenced to the user who requested it, and recall it when there’ s a
38
38
new push subscription update is left as an exercise for the reader.
39
39
40
40
Sending Data using ``webpush() `` One Call
41
41
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42
42
43
43
In many cases, your code will be sending a single message to many
44
- recipients. There' s a " One Call" function which will make things easier.
44
+ recipients. There’ s a “ One Call” function which will make things easier.
45
45
46
46
.. code :: python
47
47
48
- from pywebpush import webpush
48
+ from pywebpush import webpush
49
49
50
- webpush(subscription_info,
51
- data,
52
- vapid_private_key = " Private Key or File Path[1]" ,
53
- vapid_claims = {" sub" : " mailto:YourEmailAddress" })
50
+ webpush(subscription_info,
51
+ data,
52
+ vapid_private_key = " Private Key or File Path[1]" ,
53
+ vapid_claims = {" sub" : " mailto:YourEmailAddress" })
54
54
55
55
This will encode ``data ``, add the appropriate VAPID auth headers if
56
56
required and send it to the push server identified in the
57
57
``subscription_info `` block.
58
58
59
59
**Parameters **
60
60
61
- *subscription \_ info * - The ``dict `` of the subscription info (described
61
+ *subscription_info * - The ``dict `` of the subscription info (described
62
62
above).
63
63
64
64
*data * - can be any serial content (string, bit array, serialized JSON,
65
65
etc), but be sure that your receiving application is able to parse and
66
- understand it. (e.g. ``data = "Mary had a little lamb." ``)
66
+ understand it. (e.g. ``data = "Mary had a little lamb."``)
67
67
68
- *content \_ type * - specifies the form of Encryption to use, either
68
+ *content_type * - specifies the form of Encryption to use, either
69
69
``'aes128gcm' `` or the deprecated ``'aesgcm' ``. NOTE that not all User
70
70
Agents can decrypt ``'aesgcm' ``, so the library defaults to the RFC 8188
71
71
standard form.
72
72
73
- *vapid \_ claims * - a ``dict `` containing the VAPID claims required for
73
+ *vapid_claims * - a ``dict `` containing the VAPID claims required for
74
74
authorization (See
75
- `py \_ vapid <https://github.com/web-push-libs/vapid/tree/master/python >`__
75
+ `py_vapid <https://github.com/web-push-libs/vapid/tree/master/python >`__
76
76
for more details). If ``aud `` is not specified, pywebpush will attempt
77
77
to auto-fill from the ``endpoint ``.
78
78
79
- *vapid \_ private \_ key * - Either a path to a VAPID EC2 private key PEM
80
- file, or a string containing the DER representation. (See
81
- `py \_ vapid <https://github.com/web-push-libs/vapid/tree/master/python >`__
79
+ *vapid_private_key * - Either a path to a VAPID EC2 private key PEM file,
80
+ or a string containing the DER representation. (See
81
+ `py_vapid <https://github.com/web-push-libs/vapid/tree/master/python >`__
82
82
for more details.) The ``private_key `` may be a base64 encoded DER
83
83
formatted private key, or the path to an OpenSSL exported private key
84
84
file.
85
85
86
- e.g. the output of:
86
+ e.g. the output of:
87
87
88
88
::
89
89
90
- openssl ecparam -name prime256v1 -genkey -noout -out private_key.pem
90
+ openssl ecparam -name prime256v1 -genkey -noout -out private_key.pem
91
91
92
92
**Example **
93
93
94
94
.. code :: python
95
95
96
- from pywebpush import webpush, WebPushException
97
-
98
- try :
99
- webpush(
100
- subscription_info = {
101
- " endpoint" : " https://push.example.com/v1/12345" ,
102
- " keys" : {
103
- " p256dh" : " 0123abcde..." ,
104
- " auth" : " abc123..."
105
- }},
106
- data = " Mary had a little lamb, with a nice mint jelly" ,
107
- vapid_private_key = " path/to/vapid_private.pem" ,
108
- vapid_claims = {
109
- " sub" : " mailto:[email protected] " ,
110
- }
111
- )
112
- except WebPushException as ex:
113
- print (" I'm sorry, Dave, but I can't do that: {} " , repr (ex))
114
- # Mozilla returns additional information in the body of the response.
115
- if ex.response and ex.response.json():
116
- extra = ex.response.json()
117
- print (" Remote service replied with a {} :{} , {} " ,
118
- extra.code,
119
- extra.errno,
120
- extra.message
121
- )
96
+ from pywebpush import webpush, WebPushException
97
+
98
+ try :
99
+ webpush(
100
+ subscription_info = {
101
+ " endpoint" : " https://push.example.com/v1/12345" ,
102
+ " keys" : {
103
+ " p256dh" : " 0123abcde..." ,
104
+ " auth" : " abc123..."
105
+ }},
106
+ data = " Mary had a little lamb, with a nice mint jelly" ,
107
+ vapid_private_key = " path/to/vapid_private.pem" ,
108
+ vapid_claims = {
109
+ " sub" : " mailto:[email protected] " ,
110
+ }
111
+ )
112
+ except WebPushException as ex:
113
+ print (" I'm sorry, Dave, but I can't do that: {} " , repr (ex))
114
+ # Mozilla returns additional information in the body of the response.
115
+ if ex.response and ex.response.json():
116
+ extra = ex.response.json()
117
+ print (" Remote service replied with a {} :{} , {} " ,
118
+ extra.code,
119
+ extra.errno,
120
+ extra.message
121
+ )
122
122
123
123
Methods
124
124
~~~~~~~
@@ -145,13 +145,13 @@ Send the data using additional parameters. On error, returns a
145
145
*ttl * Message Time To Live on Push Server waiting for the client to
146
146
reconnect (in seconds)
147
147
148
- *gcm \_ key * Google Cloud Messaging key (if using the older GCM push
148
+ *gcm_key * Google Cloud Messaging key (if using the older GCM push
149
149
system) This is the API key obtained from the Google Developer Console.
150
150
151
- *reg \_ id * Google Cloud Messaging registration ID (will be extracted from
151
+ *reg_id * Google Cloud Messaging registration ID (will be extracted from
152
152
endpoint if not specified)
153
153
154
- *content \_ encoding * ECE content encoding type (defaults to " aes128gcm" )
154
+ *content_encoding * ECE content encoding type (defaults to “ aes128gcm” )
155
155
156
156
*curl * Do not execute the POST, but return as a ``curl `` command. This
157
157
will write the encrypted content to a local file named
@@ -167,7 +167,7 @@ to send from Chrome using the old GCM mode:
167
167
168
168
.. code :: python
169
169
170
- WebPusher(subscription_info).send(data, headers, ttl, gcm_key)
170
+ WebPusher(subscription_info).send(data, headers, ttl, gcm_key)
171
171
172
172
``.encode(data, content_encoding="aes128gcm") ``
173
173
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -179,19 +179,19 @@ Encode the ``data`` for future use. On error, returns a
179
179
180
180
*data * Binary string of data to send
181
181
182
- *content \_ encoding * ECE content encoding type (defaults to " aes128gcm" )
182
+ *content_encoding * ECE content encoding type (defaults to “ aes128gcm” )
183
183
184
184
**Example **
185
185
186
186
.. code :: python
187
187
188
- encoded_data = WebPush(subscription_info).encode(data)
188
+ encoded_data = WebPush(subscription_info).encode(data)
189
189
190
190
Stand Alone Webpush
191
191
-------------------
192
192
193
- If you' re not really into coding your own solution, there' s also a
194
- " stand-alone" ``pywebpush `` command in the ./bin directory.
193
+ If you’ re not really into coding your own solution, there’ s also a
194
+ “ stand-alone” ``pywebpush `` command in the ./bin directory.
195
195
196
196
This uses two files: \* the *data * file, which contains the message to
197
197
send, in whatever form you like. \* the *subscription info * file, which
@@ -201,24 +201,24 @@ like:
201
201
202
202
.. code :: json
203
203
204
- {"endpoint" : " https://push..." ,
205
- "keys" : {
206
- "auth" : " ab01..." ,
207
- "p256dh" : " aa02..."
208
- }}
204
+ {"endpoint" : " https://push..." ,
205
+ "keys" : {
206
+ "auth" : " ab01..." ,
207
+ "p256dh" : " aa02..."
208
+ }}
209
209
210
- If you' re interested in just testing your applications WebPush
210
+ If you’ re interested in just testing your applications WebPush
211
211
interface, you could use the Command Line:
212
212
213
213
.. code :: bash
214
214
215
- ./bin/pywebpush --data stuff_to_send.data --info subscription.info
215
+ ./bin/pywebpush --data stuff_to_send.data --info subscription.info
216
216
217
217
which will encrypt and send the contents of ``stuff_to_send.data ``.
218
218
219
219
See ``./bin/pywebpush --help `` for available commands and options.
220
220
221
- .. |Build Status | image :: https://travis-ci.org/web-push-libs/pywebpush.svg?branch=master
221
+ .. |Build Status | image :: https://travis-ci.org/web-push-libs/pywebpush.svg?branch=main
222
222
:target: https://travis-ci.org/web-push-libs/pywebpush
223
- .. |Requirements Status | image :: https://requires.io/github/web-push-libs/pywebpush/requirements.svg?branch=master
224
- :target: https://requires.io/github/web-push-libs/pywebpush/requirements/?branch=master
223
+ .. |Requirements Status | image :: https://requires.io/github/web-push-libs/pywebpush/requirements.svg?branch=main
224
+ :target: https://requires.io/github/web-push-libs/pywebpush/requirements/?branch=main
0 commit comments