1. 程式人生 > >測試需要修改系統時間之後, 引起https證書過期問題

測試需要修改系統時間之後, 引起https證書過期問題

開發測試的時候,我們經常需要倒退時間。倒推時間,請求https相關資源的時候,自然引起證書的問題:

    return self.__request(url)
  File "build/bdist.linux-x86_64/egg/curl/__init__.py", line 87, in __request
    self.handle.perform()
error: (60, 'Peer certificate cannot be authenticated with known CA certificates')

curl -v https://api.weixin.qq.com/sns/oauth2/access_token

* About to connect() to api.weixin.qq.com port 443 (#0)
*   Trying 183.61.49.149... connected
* Connected to api.weixin.qq.com (183.61.49.149) port 443 (#0)
* Initializing NSS with certpath: /etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* Remote Certificate has expired.
* NSS error -8181
* Closing connection #0
* Peer certificate cannot be authenticated with known CA certificates
curl: (60) Peer certificate cannot be authenticated with known CA certificates
More details here: http://curl.haxx.se/docs/sslcerts.html


curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.

證書過期了,最簡單的辦法,就是不驗證證書。如下:

#coding=utf8
#!/bin/python2.6

import pycurl
import curl

url = "https://api.weixin.qq.com/sns/oauth2/access_token"
cc = curl.Curl()
cc.set_option(pycurl.SSL_VERIFYPEER, 0)  # 加這一行
r = cc.get(url)
print r