SoFunction
Updated on 2024-11-15

Introduction to Python's pycurl package usage

pycurl is a powerful python url package, is written in c, very fast, faster than urllib and httplib both

Call method:

import pycurl

c = ()
(, '/statuses/public_timeline.xml')
import StringIO  # This uses the write function inside
b = ()
(, ) # Register the StringIO write function to pycurl's WRITEFUNCTION, i.e., all content fetched by pycurl is written to StringIO, without this sentence, pycurl outputs all content in the default exporter
()
print ()

 

Here's a small example for getting the real address of a popular short address on Twitter

import StringIO
import pycurl

c = ()
str = ()
(, "/aKln8T")
(, )
(, 1)

()
print (pycurl.EFFECTIVE_URL)

 

Visible pycurl is very powerful and concise, just to be familiar with its many attributes, the following look at some commonly used:

() # Methods to create a pycurl object
(, ) #Set the URL to be accessed
().setopt(, 5) #Set the maximum number of redirects
().setopt(, 60)
().setopt(, 300) # connection timeout setting
().setopt(, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)") #emulate browser
().perform() # server-side return information
().getinfo(pycurl.HTTP_CODE) # see 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 between when the connection is made and when the transfer begins
pycurl.STARTTRANSFER_TIME The time when the first byte was received
pycurl.TOTAL_TIME Total time of last request
pycurl.REDIRECT_TIME Time spent if steering is present
pycurl.EFFECTIVE_URL
pycurl.HTTP_CODE HTTP Response Code
pycurl.REDIRECT_COUNT Number of redirections
pycurl.SIZE_UPLOAD Size of uploaded data
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 Type of content
pycurl.RESPONSE_CODE Response Code
pycurl.SPEED_DOWNLOAD Download speed
pycurl.SSL_VERIFYRESULT
pycurl.INFO_FILETIME Time information for the 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.FTP_ENTRY_PATH