1

iOS通知を送信するためのコードを実装しようとしていますが、次のエラーが発生しています:

File "E:/Python27/apnpush.py", line 9, in <module>
apns.gateway_server.send_notification(token_hex, payload)
File "E:\Python27\lib\site-packages\apns.py", line 544, in send_notification
self.write(self._get_notification(token_hex, payload))
File "E:\Python27\lib\site-packages\apns.py", line 273, in write
return self._connection().write(string)
File "E:\Python27\lib\site-packages\apns.py", line 254, in _connection
self._connect()
File "E:\Python27\lib\site-packages\apns.py", line 230, in _connect
self._ssl = wrap_socket(self._socket, self.key_file, self.cert_file)
File "E:\Python27\lib\ssl.py", line 891, in wrap_socket
ciphers=ciphers)
File "E:\Python27\lib\ssl.py", line 509, in __init__
self._context.load_cert_chain(certfile, keyfile)
IOError: [Errno 9] Bad file descriptor

私のコードは次のとおりです。

import time
from apns import APNs, Frame, Payload
apns = APNs(use_sandbox=True, cert_file='pushchatcert_production_ori.pem', key_file='pushchatkey_production_ori.pem')
# Send a notification
token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87'
payload = Payload(alert="Hello World!", sound="default", badge=1)
apns.gateway_server.send_notification(token_hex, payload)

# Send multiple notifications in a single transmission
frame = Frame()
identifier = 1
expiry = time.time()+3600
priority = 10
frame.add_item('b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87', payload, identifier, expiry, priority)
apns.gateway_server.send_notification_multiple(frame)

また、この機能を実装しようとしてssl.wrap_socket()も、同じエラーが発生します。この 2 番目のメソッド コードは

import ssl
import json
import socket
import struct
import binascii
from ssl import wrap_socket, CERT_NONE, SSLError, PROTOCOL_SSLv23
from ssl import SSLContext
TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
PAYLOAD = {
'aps': {
    'alert': 'Hello Push!',
    'sound': 'default'
}
}
def send_push(token, payload):               
    cert='ck.pem'
    # APNS development server
    apns_address = ('gateway.sandbox.push.apple.com', 2195)
    # Use a socket to connect to APNS over SSL
    s = socket.socket()
    sock = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_SSLv3, certfile=cert)
    sock.connect(apns_address)
    # Generate a notification packet
    token = binascii.unhexlify(token)
    fmt = '!cH32sH{0:d}s'.format(len(payload))
    cmd = '\x00'
    message = struct.pack(fmt, cmd, len(token), token, len(payload), payload)
    sock.write(message)
    sock.close()  

if __name__ == '__main__':
    send_push(TOKEN, json.dumps(PAYLOAD))

このエラーを解決するにはどうすればよいですか?

4

0 に答える 0