Pycurl is a powerful Python URL package, written in C, fast, faster than Urllib and Httplib
Call Method:
Import PYCURLC = Pycurl. Curl () c.setopt (Pycurl. URL, ' http://api.minicloud.com.cn/statuses/public_timeline.xml ') import stringio #这个用到里面的write函数b = Stringio.stringio () c.setopt (Pycurl. Writefunction, B.write) #把StringIO的写函数注册到pycurl的WRITEFUNCTION中, that is, pycurl all acquired content is written to Stringio, without this sentence, Pycurl will output all the content in the default output C.perform () print b.getvalue ()
Here is a small example to get the real address of a popular short address on Weibo
Import Stringioimport PYCURLC = Pycurl. Curl () str = Stringio.stringio () c.setopt (Pycurl. URL, "Http://t.cn/aKln8T") c.setopt (Pycurl. Writefunction, Str.write) c.setopt (Pycurl. Followlocation, 1) c.perform () print c.getinfo (Pycurl. Effective_url)
Visible Pycurl is very powerful and concise, just to familiarize itself with many of its properties, see some commonly used:
Pycurl. Curl () #创建一个pycurl对象的方法
Pycurl. Curl (Pycurl. URL, http://www.google.com.hk) #设置要访问的URL
Pycurl. Curl (). setopt (Pycurl. Maxredirs, 5) #设置最大重定向次数
Pycurl. Curl (). setopt (Pycurl. ConnectTimeout, 60)
Pycurl. Curl (). setopt (Pycurl. TIMEOUT, #连接超时设置
Pycurl. Curl (). setopt (Pycurl. UserAgent, "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1,. NET CLR 1.1.4322) #模拟浏览器
Pycurl. Curl (). Perform () #服务器端返回的信息
Pycurl. Curl (). GetInfo (Pycurl. Http_code) #查看HTTP的状态 similar to the status attribute in Urllib
Pycurl. Namelookup_time Domain Name resolution time
Pycurl. Connect_time Remote Server Connection time
Pycurl. Pretransfer_time the time after the connection is over to the start of the transfer
Pycurl. Starttransfer_time the time that the first byte was received
Pycurl. Total_time the total time of the previous request
Pycurl. Redirect_time If there is a turn, the time spent
Pycurl. Effective_url
Pycurl. Http_code HTTP Response Code
Pycurl. Number of Redirect_count redirects
Pycurl. Size_upload size of uploaded data
Pycurl. Size_download the size of the downloaded data
Pycurl. Speed_upload upload Speed
Pycurl. Header_size Head Size
Pycurl. Request_size Request Size
Pycurl. Content_length_download Download Content Length
Pycurl. Content_length_upload Upload Content Length
Pycurl. Types of Content_Type Content
Pycurl. Response_code Response Code
Pycurl. Speed_download Download speed
Pycurl. Ssl_verifyresult
Time information for the Pycurl.info_filetime file
Pycurl. Http_connectcode HTTP Connection Code
Pycurl. Httpauth_avail
Pycurl. Proxyauth_avail
Pycurl. Os_errno
Pycurl. Num_connects
Pycurl. Ssl_engines
Pycurl.info_cookielist
Pycurl. Lastsocket
Pycurl. Ftp_entry_path