Home>

pycurl is a powerful python url package written in c language,high speed,Faster than both urllib and httplib

Calling method:

import pycurl
c=pycurl.curl ()
c.setopt (pycurl.url, "http://api.minicloud.com.cn/statuses/public_timeline.xml")
import stringio #This uses the write function inside
b=stringio.stringio ()
c.setopt (pycurl.writefunction, b.write) #Register the write function of stringio to the writefunction of pycurl, that is, all the content obtained by pycurl is written to stringio. If there is no such sentence,pycurl will output everything in the default output
c.perform ()
print b.getvalue ()

Here is a small example,Used to get the real address corresponding to the popular short address on Weibo

import stringio
import pycurl
c=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)

It can be seen that pycurl is very powerful and concise,Just to be familiar with many of its properties,Let's look at some common ones:

pycurl.curl () #method for creating a pycurl object

pycurl.curl (pycurl.url, http://www.google.com.hk) #Set the URL to visit

pycurl.curl (). setopt (pycurl.maxredirs, 5) #Set the maximum number of redirects

pycurl.curl (). setopt (pycurl.connecttimeout, 60)

pycurl.curl (). setopt (pycurl.timeout, 300) #connection timeout setting

pycurl.curl (). setopt (pycurl.useragent, "mozilla/4.0 (compatible;msie 6.0;windows nt 5.1;sv1;.net clr 1.1.4322)") #Simulate the browser

pycurl.curl (). perform () #The information returned by the server

pycurl.curl (). getinfo (pycurl.http_code) #View the status of 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 Time from connection to transfer start

pycurl.starttransfer_time time when the first byte was received

pycurl.total_time Total time of the last request

pycurl.redirect_time if there is a redirect,time spent

pycurl.effective_url

pycurl.http_code http response code

pycurl.redirect_count number of redirects

pycurl.size_upload upload data size

pycurl.size_download Downloaded data size

pycurl.speed_upload upload speed

pycurl.header_size header size

pycurl.request_size request size

pycurl.content_length_download download content length

pycurl.content_length_upload upload content length

pycurl.content_type type of content

pycurl.response_code response code

pycurl.speed_download download speed

pycurl.ssl_verifyresult

pycurl.info_filetime file time information

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

  • Previous Definition of apply and call methods and the difference between apply and call methods
  • Next C # implements three methods to simulate automatic login and submit POST information