Search code examples
pythonamazon-web-servicespyopensslhyperalpn

Cannot send ALPN request to APNS with python 3.5 and Hyper


I am trying to implement APNS service on my Python REST server with Hyper. Since APNS now require HTTP/2 (ALPN) protocol, I was not able to get_response() from Hyper connection. It throws the below error.

Traceback (most recent call last):
  File "/usr/lib64/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/lib64/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/lib64/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib64/python3.5/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/lib64/python3.5/dist-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/lib/python3.5/dist-packages/rest_framework/views.py", line 489, in dispatch
    response = self.handle_exception(exc)
  File "/usr/lib/python3.5/dist-packages/rest_framework/views.py", line 449, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/lib/python3.5/dist-packages/rest_framework/views.py", line 486, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/lib/python3.5/dist-packages/rest_framework/decorators.py", line 52, in handler
    return func(*args, **kwargs)
  File "/opt/vr-inspire/apns/views.py", line 137, in apns
    resp = conn.get_response()
  File "/usr/lib/python3.5/dist-packages/hyper/common/connection.py", line 129, in get_response
    return self._conn.get_response(*args, **kwargs)
  File "/usr/lib/python3.5/dist-packages/hyper/http11/connection.py", line 203, in get_response
    self._sock.fill()
  File "/usr/lib/python3.5/dist-packages/hyper/common/bufsocket.py", line 169, in fill
    raise ConnectionResetError()
ConnectionResetError

I did some research, and it turns out ALPN require OpenSSL 1.0.2. I compiled OpenSSL 1.0.2i and Python 3.5.1 from source with reference to this article.

I checked the ssl version with import ssl print(ssl.OPENSSL_VERSION) and returns OpenSSL 1.0.2i 22 Sep 2016. But ssl.HAS_ALPN return False.

I am using Amazon Linux, how may I use ALPN to send Request to APNS server? Thank you.


Solution

  • I try to use python 3.6.1 with openssl 1.0.2k and it works.