Today in the payment refund interface, because the need to use to two-way certificate authentication, so at first there is no clue, and later found a similar tutorial on the Internet, found the Pycurl module, the successful implementation of the certificate certification, tutorial Links: http://blog.csdn.net/ yueguanghaidao/article/details/41451711/.
Then because it was a direct copy of the code, so I took a little time to familiarize myself with this module, because I want to achieve the purpose of using the certificate post, so I wrote a function
defPostxmlssl (self, XML, URL, second=30, Cert=true, post=True):#post with a certificateCurl =Pycurl. Curl () #创建pcurl对象 curl.setopt (Pycurl. Ssl_verifyhost, False) #对于某些采用HTTPS的网站, sometimes due to failed certificate validation, the Pycurl module provides the ability to cancel the validation process. Curl.setopt (Pycurl. Ssl_verifypeer, False) curl.setopt (Pycurl. HEADER, False) #设置不输出header curl.setopt (Pycurl. URL, url) #设置url curl.setopt (Pycurl. TIMEOUT, second) #连接超时时间ifcert: #如果是存在证书, there will be a certificate and a key, Python uses two PEM certificates, then define the type, enter the certificate path is OK. Curl.setopt (Pycurl. Sslkeytype,"PEM") curl.setopt (Pycurl. Sslkey,"###") curl.setopt (Pycurl. Sslcerttype,"PEM") curl.setopt (Pycurl. Sslcert,"###") ifPost: #使用的是post方法, then the post content is in XML format. Curl.setopt (Pycurl. POST, True) curl.setopt (Pycurl. Postfields, XML) Buff=Bytesio () #python3使用的是io模块的字节流, not the Stingio in the tutorial, which is the curl.setopt used by Python2 (Pycurl. Writefunction, Buff.write) #pycurl模块不具备存储的功能, so the data is written to the byte stream Curl.perform () #执行操作returnBuff.getvalue (). Decode ("Utf-8"#返回字节流中的数据, before returning to decode, I am the data obtained from the interface, so it is decoded with Utf-8.
After the discovery of a special Pycurl module of the document, beep the dog, if you find it earlier .... Post a document link: https://mp.weixin.qq.com/s?__biz=MzIwMDYxMjgyMg==&mid=2650361142&idx=1&sn= B11ad13a718b8c91280eab1ffbe62b98
Python authentication for SSL--pycurl module use