Python pycurl package usage and pythonpycurl usage

Source: Internet
Author: User

Python pycurl package usage and pythonpycurl usage

Pycurl is a powerful python url package. It is written in C language and is 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 # Use the write function B = StringIO. stringIO () c. setopt (pycurl. WRITEFUNCTION, B. write) # register the StringIO write function to the WRITEFUNCTION of pycurl, that is, all the obtained content of pycurl is written to StringIO, pycurl will output all the content in the default output device c. perform () print B. getvalue ()

 

Here is a small example to get the real address corresponding to the 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)

 

It can be seen that pycurl is very powerful and concise, but you need to be familiar with many of its attributes. Here are some common ones:

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 times
Pycurl. 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) # view the HTTP status 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 from the connection to the start of transmission
Time when pycurl. STARTTRANSFER_TIME receives the first byte
Total time of the request on pycurl. TOTAL_TIME
Pycurl. REDIRECT_TIME if there is a redirection, the time spent
Pycurl. inclutive_url
Pycurl. HTTP_CODE HTTP response code
Pycurl. REDIRECT_COUNT redirect times
Data size Uploaded By pycurl. SIZE_UPLOAD
Pycurl. SIZE_DOWNLOAD 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 content type
Pycurl. RESPONSE_CODE response code
Pycurl. SPEED_DOWNLOAD download speed
Pycurl. SSL_VERIFYRESULT
Time information of 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.