Home >Backend Development >Python Tutorial >Detailed introduction to using curl library pycurl examples and parameters
pycurl is a powerful python url library, written in c language. It is very fast, faster than urllib and httplib.
Today we look at the usage and parameters of pycurl in detail
Commonly used methods:
pycurl.Curl() #Method to create a pycurlobject
pycurl.Curl().setopt(pycurl.URL, http://www.pythontab.com) #Set the URL to be accessed
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/5.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)") #Simulated browsing Device
pycurl.Curl().perform() #Information returned by the server
pycurl.Curl().getinfo(pycurl.HTTP_CODE) #View HTTP statusSimilar to the status attribute in urllib
pycurl.NAMELOOKUP_TIME domain name resolution time
pycurl.CONNECT_TIME remote server connection time
pycurl.PRETRANSFER_TIME time after connection to start of transmission
pycurl.STARTTRANSFER_TIME The time to receive the first byte
pycurl.TOTAL_TIME The total time of the previous request
pycurl.REDIRECT_TIME If there is a redirection, the time it takes
pycurl.EFFECTIVE_URL
pycurl.HTTP_CODE HTTP response code
pycurl.REDIRECT_COUNT Number of redirects
pycurl.SIZE_UPLOAD Data size of upload
pycurl.SIZE_DOWNLOAD Size of downloaded data
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 Content type
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.LASTSOCKET
pycurl.FTP_ENTRY_PATH
Example:
import StringIO import pycurl c = pycurl.Curl() str = StringIO.StringIO() c.setopt(pycurl.URL, "http://www.pythontab.com") c.setopt(pycurl.WRITEFUNCTION, str.write) c.setopt(pycurl.FOLLOWLOCATION, 1) c.perform() print c.getinfo(pycurl.EFFECTIVE_URL)
Friends who are familiar with PHP may have discovered that the usage of this curl library is very similar to PHP's curl.
The above is the detailed content of Detailed introduction to using curl library pycurl examples and parameters. For more information, please follow other related articles on the PHP Chinese website!