Python中的CURL PycURL使用例子
更新時間:2014年06月01日 22:43:59 作者:
這篇文章主要介紹了Python中的CURL PycURL使用例子,需要的朋友可以參考下
在Linux上有個常用的命令 curl(非常好用),支持curl的就是大名鼎鼎的libcurl庫;libcurl是功能強大的,而且是非常高效的函數(shù)庫。libcurl除了提供本身的C API之外,還有多達40種編程語言的Binding,這里介紹的PycURL就是libcurl的Python binding。
在Python中對網(wǎng)頁進行GET/POST等請求,當需要考慮高性能的時候,libcurl是非常不錯的選擇,一般來說會比liburl、liburl2快不少,可能也會比Requests的效率更高。特別是使用PycURL的多并發(fā)請求時,更是效率很高的。個人感覺,其唯一的缺點是,由于是直接調(diào)用的是libcurl C庫,PycURL的函數(shù)接口之類的還和C中的東西很像,可能不是那么的Pythonic,寫代碼的學習曲線稍微比liburl高一點兒。
還是看個簡單的例子吧:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on Dec 15, 2013
@author: Jay
'''
import sys
import pycurl
import time
class Test:
def __init__(self):
self.contents = ''
def body_callback(self, buf):
self.contents = self.contents + buf
sys.stderr.write("Testing %s\n" % pycurl.version)
start_time = time.time()
url = 'http://www.dianping.com/shanghai'
t = Test()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEFUNCTION, t.body_callback)
c.perform()
end_time = time.time()
duration = end_time - start_time
print c.getinfo(pycurl.HTTP_CODE), c.getinfo(pycurl.EFFECTIVE_URL)
c.close()
print 'pycurl takes %s seconds to get %s ' % (duration, url)
print 'lenth of the content is %d' % len(t.contents)
#print(t.contents)
在Python中對網(wǎng)頁進行GET/POST等請求,當需要考慮高性能的時候,libcurl是非常不錯的選擇,一般來說會比liburl、liburl2快不少,可能也會比Requests的效率更高。特別是使用PycURL的多并發(fā)請求時,更是效率很高的。個人感覺,其唯一的缺點是,由于是直接調(diào)用的是libcurl C庫,PycURL的函數(shù)接口之類的還和C中的東西很像,可能不是那么的Pythonic,寫代碼的學習曲線稍微比liburl高一點兒。
還是看個簡單的例子吧:
復制代碼 代碼如下:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on Dec 15, 2013
@author: Jay
'''
import sys
import pycurl
import time
class Test:
def __init__(self):
self.contents = ''
def body_callback(self, buf):
self.contents = self.contents + buf
sys.stderr.write("Testing %s\n" % pycurl.version)
start_time = time.time()
url = 'http://www.dianping.com/shanghai'
t = Test()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEFUNCTION, t.body_callback)
c.perform()
end_time = time.time()
duration = end_time - start_time
print c.getinfo(pycurl.HTTP_CODE), c.getinfo(pycurl.EFFECTIVE_URL)
c.close()
print 'pycurl takes %s seconds to get %s ' % (duration, url)
print 'lenth of the content is %d' % len(t.contents)
#print(t.contents)
相關文章
Python3實現(xiàn)打格點算法的GPU加速實例詳解
這篇文章主要給大家介紹了關于Python3實現(xiàn)打格點算法的GPU加速的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用python具有一定的參考學習價值,需要的朋友可以參考下2021-09-09基于PyQt5實現(xiàn)狀態(tài)欄(statusBar)顯示和隱藏功能
這篇文章主要為大家詳細介紹了如何利用PyQt5實現(xiàn)狀態(tài)欄顯示和隱藏功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-08-08詳解在Python的Django框架中創(chuàng)建模板庫的方法
這篇文章主要介紹了在Python的Django框架中創(chuàng)建模板庫的方法,模版庫通常用來管理單獨的Django中的應用,需要的朋友可以參考下2015-07-07python3 dict ndarray 存成json,并保留原數(shù)據(jù)精度的實例
今天小編就為大家分享一篇python3 dict ndarray 存成json,并保留原數(shù)據(jù)精度的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12