我正在尝试实施推送通知。我可以通过此调用触发需要从python发出的通知:
curl -X POST -H "Content-Type: application/json" -H "X-Thunder-Secret-Key: secret2" --data-ascii "\"Hello World\"" http://localhost:8001/api/1.0.0/key2/channels/mychannel/
从命令行可以正常工作。
首先,我尝试使用子流程,但它给了我这个奇怪的错误:
curl: (1) Protocol "http not supported or disabled in libcurl
所以我放弃了,我尝试使用pycurl。但是问题是我不知道如何使用-X和--data-ascii选项。
import pycurl
c = pycurl.Curl()
c.setopt(c.HTTPHEADER, ['Content-Type: application/json','X-Thunder-Secret-Key: secret2'])
c.setopt(c.URL, 'http://localhost:8001/api/1.0.0/key2/channels/mychannel/')
c.perform()
print("Done")
那么,如何添加-X选项以及如何发送带有请求的文本消息?
我认为类似这样的东西应该可以工作(我使用过python 2):
import pycurl
c = pycurl.Curl()
postfields = '"Hello World"'
c.setopt(c.URL, 'http://pycurl.sourceforge.net/tests/testpostvars.php')
c.setopt(c.HTTPHEADER, ['Content-Type: application/json','X-Thunder-Secret-Key: secret2'])
# Here we set data for POST request
c.setopt(c.POSTFIELDS, postfields)
c.perform()
c.close()
此代码产生以下HTTP数据包:
POST /tests/testpostvars.php HTTP/1.1
User-Agent: PycURL/7.19.5.1 libcurl/7.37.1 SecureTransport zlib/1.2.5
Host: pycurl.sourceforge.net
Accept: */*
Content-Type: application/json
X-Thunder-Secret-Key: secret2
Content-Length: 13
"Hello World"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句