Python pycurl and pythonpycurl
Common Methods:
pycurl.Curl()
# Method for creating a pycurl object
pycurl.Curl(pycurl.URL, http:
//www
.google.com.hk)
# Set the URL to be accessed
pycurl.Curl().setopt(pycurl.MAXREDIRS, 5)
# Set the maximum number of redirection timesPycurl. Curl (). setopt (pycurl. CONNECTTIMEOUT, 60)
pycurl.Curl().setopt(pycurl.TIMEOUT, 300)
# Connection timeout settings
pycurl.Curl().setopt(pycurl.USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"
)
# Simulate a browser
pycurl.Curl().perform()
# Information returned by the server
pycurl.Curl().getinfo(pycurl.HTTP_CODE)
# Viewing the HTTP status is similar to the status attribute in urllibPycurl. NAMELOOKUP_TIME domain name resolution time pycurl. CONNECT_TIME remote server connection time pycurl. the time from PRETRANSFER_TIME connection to start transmission pycurl. STARTTRANSFER_TIME receives the time of the first byte pycurl. total time of the request on TOTAL_TIME pycurl. REDIRECT_TIME if there is a redirection, it takes time to pycurl. EFFECTIVE_URLpycurl.HTTP_CODE HTTP response code pycurl. REDIRECT_COUNT redirect times pycurl. the size of the data uploaded by SIZE_UPLOAD is pycurl. SIZE_DOWNLOAD the data size pycurl. SPEED_UPLOAD upload speed: pycurl. HEADER_SIZE header size pycurl. request size: pycurl. CONTENT_LENGTH_DOWNLOAD download content length: pycurl. CONTENT_LENGTH_UPLOAD the length of the uploaded content pycurl. the CONTENT_TYPE content type pycurl. RESPONSE_CODE response code pycurl. SPEED_DOWNLOAD download speed: pycurl. the time information of the SSL_VERIFYRESULTpycurl.INFO_FILETIME file pycurl. HTTP_CONNECTCODE HTTP connection code pycurl. example: Get the real URL of a Weibo short URL
1 import StringIO 2 import pycurl 3 4 c = pycurl.Curl() 5 str = StringIO.StringIO() 6 c.setopt(pycurl.URL, "http://t.cn/RLlwzRh") 7 c.setopt(pycurl.WRITEFUNCTION, str.write) 8 c.setopt(pycurl.FOLLOWLOCATION, 1) 9 10 c.perform()11 print c.getinfo(pycurl.EFFECTIVE_URL)