One - One Code All

Blog Content

用pycurl请求https

Python   2015-09-21 13:02:04
import pycurl
import json
from io import *
try:
    c = pycurl.Curl()
    b = BytesIO()
    c.setopt(pycurl.WRITEFUNCTION,b.write)
    c.setopt(c.URL, 'https://baidu.com')
    c.setopt(pycurl.SSL_VERIFYPEER, 1)
    c.setopt(pycurl.SSL_VERIFYHOST, 2)
    #windows 要指定证书的路径不然会出现(77, "SSL: can't load CA certificate file E:\\curl\\ca-bundle.crt")
    #证书路径就在curl下载的压缩包里面。mac/linux下面可以注释掉。
    c.setopt(pycurl.CAINFO, "E:\curl\ca-bundle.crt")
    #
    c.perform()
    result = b.getvalue().decode("utf-8")
    print(result)
except BaseException as e:
    print(e)
finally:
    b.close()
    c.close()



上一篇:python内存缓存管理wrapcache
下一篇:python中pandas的dataframe对象的创建及矩阵模拟赋值

The minute you think of giving up, think of the reason why you held on so long.