Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 套接字错误“[Errno 9]错误的文件描述符”_Python_Sockets_Ssl_Apple Push Notifications_Apn - Fatal编程技术网

Python 套接字错误“[Errno 9]错误的文件描述符”

Python 套接字错误“[Errno 9]错误的文件描述符”,python,sockets,ssl,apple-push-notifications,apn,Python,Sockets,Ssl,Apple Push Notifications,Apn,我正在尝试实现用于发送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_notifica

我正在尝试实现用于发送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
我还尝试用ssl.wrap_socket实现此功能,但还是遇到了相同的错误。第二种方法代码是

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))
如何解决这个错误

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))