The installation method of the Pycurl module is as follows:easy_install pycurl #easy_install安装方法pip install pycurl #pip安装方法 # Source installation method # require Curl-config package support, need to re-install the source method curl# wget http://curl.haxx.se/download/curl-7.36.0.tar.gz# tar -zxvf curl-7.36.0.tar.gz# cd curl-7.36.0# ./configure# make & & make install# export ld_library_path=/usr/local/lib## wget https:// pypi.python.org/packages/source/p/pycurl/pycurl-7.19.3.1.tar.gz --no-check-certificate# tar - zxvf pycurl-7.19.3.1.tar.gz# cd pycurl-7.19.3.1# python setup.py install -- Curl-config=/usr/local/bin/curl-config Module Common method Description Pycurl. The curl () class implements a curl handle object that creates a Libcurl package with no arguments. See http://curl.haxx.se/libcurl/c/libcurltutorial.html For more information about the Libcurl package. Here are a few common methods of Curl objects. The close () method, corresponding to the Curl_easy_cleanup method in the Libcurl package, has no parameters to implement the close, Recycle Curl object. perform () method, corresponding to Libcurl in Curl_ package The Easy_perform method, without parameters, implements a Curl object request submission. Setopt (Option,value) method, corresponding to Curl_easy_setopt method in Libcurl package, parameteroption is specified by a constant of libcurl. The value of the parameter value is dependent on option, which can be a string, integer, Long integer, file object, list, function, and so on. A list of commonly used constants is listed below: Pycurl. Curl () #创建一个curl对象c. setopt (Pycurl. connecttimeout, 5) #连接的等待时间, set to 0 without waiting for c.setopt (Pycurl. timeout, 5) #请求超时时间c. setopt (Pycurl. noprogress, 0) #是否屏蔽下载进度条, not 0 shielded c.setopt (pycurl. maxredirs, 5) #指定HTTP重定向的最大数c. setopt (Pycurl. forbid_reuse, 1) #完成交互后强制断开连接, do not reuse c.setopt (Pycurl. fresh_connect,1) #强制获取新的连接, which is the replacement of the connection c.setopt in the cache (Pycurl. dns_cache_timeout,60) #设置保存DNS信息的时间, default is 120 seconds c.setopt (Pycurl. URL, "http://www.baidu.com") #指定请求的URLc. setopt (Pycurl. UserAgent, "mozilla/5.2 (compatible; msie 6.0; windowsnt 5.1; sv1; .net clr 1.1.4322; .net clr 2.0.50324) #配置请求HTTP头的User-agentc.setopt (Pycurl. Headerfunction, getheader) #将返回的HTTP header directed to the callback function getheaderc.setopt (Pycurl. Writefunction, getbody) #将返回的内容定向到回调函数getbodyc. setopt (Pycurl. writeheader, fileobj) #将返回的HTTP &nbsP The header is directed to the Fileobj file Object C.setopt (Pycurl. writedata, fileobj) #将返回的HTML内容定向到fileobj文件对象 getinfo (option) method, corresponding to the Curl_easy_getinfo method in the Libcurl package, The parameter option is specified by a constant of Libcurl. A list of commonly used constants is listed below: C = pycurl. Curl () #创建一个curl对象c. GetInfo (Pycurl. Http_code) #返回的HTTP状态码c. GetInfo (Pycurl. Total_time) #传输结束所消耗的总时间c. GetInfo (Pycurl. Namelookup_time) #DNS解析所消耗的时间c. GetInfo (Pycurl. Connect_time) #建立连接所消耗的时间c. GetInfo (Pycurl. Pretransfer_time) #从建立连接到准备传输所消耗的时间c. GetInfo (Pycurl. Starttransfer_time) #从建立连接到传输开始消耗的时间c. GetInfo (Pycurl. Redirect_time) #重定向所消耗的时间c. GetInfo (Pycurl. Size_upload) #上传数据包大小c. GetInfo (Pycurl. Size_download) #下载数据包大小c. GetInfo (Pycurl. Speed_download) #平均下载速度c. GetInfo (Pycurl. Speed_upload) #平均上传速度c. GetInfo (Pycurl. header_size) #HTTP头部大小
#!/usr/bin/env python#_*_coding:utf-8 _*___author__ = ' Gaogd ' Import os,sysimport timeimport sysimport pycurlurl= "http://www.baidu.com" #探测的目标URLc = pycurl. Curl () #创建一个Curl对象c. setopt (Pycurl. Url, url) # Defines the URL constant for the request c.setopt (Pycurl. connecttimeout, 5) #定义请求连接的等待时间c. setopt (Pycurl. TIMEOUT, 5) # Defines the request time-out time c.setopt (Pycurl. noprogress, 1) #屏蔽下载进度条c. setopt (Pycurl. forbid_reuse, 1) #完成交互后强制断开连接, do not reuse c.setopt (Pycurl. maxredirs, 1) #指定HTTP重定向的最大数为1c. Setopt ( Pycurl. dns_cache_timeout,30) #设置保存DNS信息的时间为30秒 # Create a File object that opens in "WB" to store the returned HTTP header and page contentsIndexfile = open (Os.path.dirname (Os.path.realpath (__file__)) + "/content.txt", "WB") C.setopt (Pycurl. Writeheader, indexfile) #将返回的HTTP The header is directed to the Indexfile file Object C.setopt (Pycurl. Writedata, indexfile) # Directs the returned HTML content to the Indexfile file object try: #pass c.perform () # Submit Request except exception as e: print "Connecion error:" +str (E) indexfile.close () c.close () sys.exit () Namelookup_time = c.getinfo (c.namelookup_time) #获取DNS解析时间CONNECT_TIME = c.getinfo (c. Connect_time) #获取建立连接时间PRETRANSFER_TIME = c.getinfo (c.pretransfer_time) # Gets the time consumed from establishing the connection to preparing the transfer Starttransfer_time = c.getinfo (c.starttransfer_time) #获取从建立连接到传输开始消耗的时间TOTAL_ Time = c.getinfo(c.total_time) #获取传输的总时间HTTP_CODE = c.getinfo (c.http_code) #获取HTTP状态码SIZE_DOWNLOAD = c.getinfo (c.size_download) #获取下载数据包大小HEADER_SIZE = c.getinfo (c.header_size) # Get HTTP Header size Speed_download=c.getinfo (c.speed_download) #获取平均下载速度 # printout related Data print "HTTP status code:%s" % ( HTTP_CODE) print "DNS resolution Time:%.2f ms"% (namelookup_time*1000) print "Establish connection time:%.2f ms" % ( connect_time*1000) print "Ready to transfer time:%.2f ms" % (pretransfer_time*1000) print "Transfer start time:%.2f MS " % (starttransfer_time*1000) print " Transfer End total time:%.2f ms " % (total_time*1000) print " Download packet size:%d bytes/s " % (size_download) print " HTTP header size:%d byte " % (header_size) print "Average download speed:%d bytes/s" % (speed_download) #关闭文件及Curl对象indexfile. Close () C.close ()
This article is from the "Struggle Bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1871808
Python Pycurl Module Learning