简体   繁体   English

相当于php的stream_context_create的Python

[英]Python equivalent to php's stream_context_create

I am following a tutorial that requires me to open a socket and send a payload with a certificate: http://blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/ . 我正在遵循一个要求我打开套接字并发送带有证书的有效负载的教程: http : //blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/

I am having a bit of difficulty in figuring out how to do the following with python: 我在弄清楚如何使用python执行以下操作时遇到了一些困难:

// Open the connection
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

// Send the payload
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);

// Close Resources
socket_close($apns);
fclose($apns);

I've used python-requests in the past. 我过去使用过python-requests。 But only for simple get requests. 但仅适用于简单的获取请求。

Can I use python-requests to rewrite the above code in python, or is there something better suited for this task? 我可以使用python-requests在python中重写以上代码,还是有一些更适合此任务的东西?

Looking at the PHP documentation fo stream_context_create, and the tutorial you're following I think what you're trying to accomplish is opening a SSL secured socket connection to gatway.sandbox.push.apple.com. 在查看PHP文档fo stream_context_create和您正在遵循的教程时,我认为您要完成的工作是打开与gatway.sandbox.push.apple.com的SSL安全套接字连接。

I think the python module you want to look at is the ssl module . 我认为您要查看的python模块是ssl模块 There's an example of connecting to a server from a client, probably setting the keyfile and certfile options of ssl.wrap_socket() to the certificate you created and registered to talk to the apple server. 有一个从客户端连接到服务器的示例 ,可能将ssl.wrap_socket()的keyfile和certfile选项设置为您创建并注册以与Apple服务器对话的证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM