etataurov / asyncio-apns Goto Github PK
View Code? Open in Web Editor NEWasyncio client for Apple Push Notification Service
License: MIT License
asyncio client for Apple Push Notification Service
License: MIT License
Cannot send 34 bytes, flow control window is 17
Add example code to show how to use asyncio-apns with Tornado
I'm getting the following traceback when trying to essentially follow the minimal example from the README:
» venv/bin/python test_justin.py
Exception in callback _SelectorSocketTransport._read_ready()
handle: <Handle _SelectorSocketTransport._read_ready()>
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/events.py", line 145, in _run
self._callback(*self._args)
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/selector_events.py", line 730, in _read_ready
self._protocol.data_received(data)
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/sslproto.py", line 514, in data_received
self._app_protocol.data_received(chunk)
File "/Users/roy/tmp/apns/venv/lib/python3.6/site-packages/asyncio_apns/h2_client.py", line 101, in data_received
self.handle_response(event.stream_id)
File "/Users/roy/tmp/apns/venv/lib/python3.6/site-packages/asyncio_apns/h2_client.py", line 204, in handle_response
status_code = int(headers[":status"])
If I set a breakpoint at that location, headers
, somewhat oddly, seems to contain bytestrings:
» venv/bin/python test_justin.py
> /Users/roy/tmp/apns/test_justin.py(12)send_push()
11 import ipdb; ipdb.set_trace()
---> 12 apns = await connect(CERT_FILE, KEY_FILE, development=True, loop=loop)
13 result = await apns.send_message(message, token)
ipdb> b /Users/roy/tmp/apns/venv/lib/python3.6/site-packages/asyncio_apns/h2_client.py:204
Breakpoint 1 at /Users/roy/tmp/apns/venv/lib/python3.6/site-packages/asyncio_apns/h2_client.py:204
ipdb> c
> /Users/roy/tmp/apns/venv/lib/python3.6/site-packages/asyncio_apns/h2_client.py(204)handle_response()
203 data = data_event.data if data_event is not None else None
1-> 204 status_code = int(headers[":status"])
205 if status_code != 200:
ipdb> p headers
{b':status': b'200', b'apns-id': b'< redacted >'}
…which is weird. I would expect an HTTP library to decode the received headers into strings (str
) since that's the appropriate data type for that. (That is, headers
seems to not be decoded enough; the code seems correct to reference the header as ':status'
)
AttributeError: 'SSLContext' object has no attribute 'set_alpn_protocols'
Python3.4 ssl module doesn't support ALPN, but APNS server requires it (doesn't work with NPN)
Possible solution:
Use PyOpenSSL and hyper compatibility wrapper https://github.com/Lukasa/hyper/blob/development/hyper/ssl_compat.py
Needs investigation
=================================== FAILURES ===================================
______________________________ test_send_message _______________________________
apns_connect = <function apns_connect.<locals>.connector at 0x7ff38af6a0d0>
@pytest.mark.asyncio
def test_send_message(apns_connect):
connection = yield from apns_connect()
token = "abcde"
message = "Hello"
connection.protocol.send_request.return_value = future_with_result(
(mock.MagicMock(), mock.MagicMock()))
> yield from connection.send_message(message, token)
tests/test_connection.py:86:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <asyncio_apns.apns_connection.APNsConnection object at 0x7ff38af75128>
payload = 'Hello', token = 'abcde'
priority = <NotificationPriority.immediate: 10>, topic = None
@asyncio.coroutine
def send_message(self, payload: Union[Payload, str], token: str,
priority: NotificationPriority = NotificationPriority.immediate,
topic: str=None):
if not self.connected:
yield from self.connect()
headers, data = self._prepare_request(payload, token, priority, topic)
try:
> headers, _ = yield from self.protocol.send_request(headers, data)
E ValueError: not enough values to unpack (expected 2, got 0)
Hi,
Your example is about sending a simple push, but in major case we need to send thousand of pushs.
I have a problem with it. I use a Semaphore for sending 50 pushs in concurrency (without it it's so slow ! about 2 pushs / sec) but i can't catch a bad token.
EG: I have a test push server, and with 188th push, i send back a 400: BadToken error.
Client side, i got the error but not for the good push/token (190th, 192th, even 189th but not the good one (188)
I have read there are something with "futures" but don't see many good examples on web about handling / playing with that.
My current testing code:
async def send_push4(apns, message, token):
with await mysem:
try:
await apns.send_message(message, token)
except Exception as APNsError:
print("Error %s for %s" %(APNsError.status, token))
loop = asyncio.get_event_loop()
sem = asyncio.Semaphore(50)
for i in range(1,200):
tasks.append(asyncio.async(send_push4(apns, message, token)))
loop.run_until_complete(asyncio.tasks)
Thanks for helping.
#6 follow up
It should be possible to use this library for sending multiple messages concurrently, but there is concurrently open streams limit https://tools.ietf.org/html/rfc7540#section-5.1.2
There should be some way to satisfy SETTINGS_MAX_CONCURRENT_STREAMS, without user knowing about this limit.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.