Callbackscurl ObjectThe Curl object has the following methods:
Close (), None
Corresponds to the Curl_easy_cleanup method in Libcurl. Pycurl calls this method automatically when the Curl object is no longer referenced, but it can also be called directly.
Perform ()-None
Corresponds to the Curl_easy_perform method in Libcurl.
Setopt (option, value), None
corresponding to the Curl_easy_setopt method in Libcurl, option is specified using the curlopt_* constant in Libcurl, but the curlopt_ prefix is now removed. The data type of value is dependent on option, It can be a string, an integer, a long integer, a file object, a list, or a function.
Examples of usage:
Import Pycurl
c = Pycurl. Curl ()
C.setopt (Pycurl. URL, "http://www.python.org/")
C.setopt (Pycurl. Httpheader, ["Accept:"])
Import Stringio
b = Stringio.stringio ()
C.setopt (Pycurl. Writefunction, B.write)
C.setopt (Pycurl. Followlocation, 1)
C.setopt (Pycurl. Maxredirs, 5)
C.perform ()
Print B.getvalue ()GetInfo (option), Result
For the Curl_easy_getinfo method in Libcurl, option is also specified using the Curlopt_* constant in Libcurl, but the curlopt_ prefix is now removed. Result contains an integer, floating-point number, or string, which is believed to be a given Option.getinfo method that cannot be called before the Perform method is called or completed.
Examples of usage:
Errstr (), String
Returns a string representation of the internal Libcurl error buffer in this processor.
Curlmulti Object
The Curlmulti object has the following methods:
Close (), None
Corresponds to the Curl_multi_cleanup () method in Libcurl. Pycurl calls the method automatically when the Curlmulti object is no longer referenced, or it can be displayed.
Perform (), tuple of status and the number of active Curl objects
Corresponds to the Curl_multi_perform () method in Libcurl.
Add_handle (Curl object), None
Corresponds to the Curl_multi_add_handle () method in Libcurl. This method adds a valid Curl object to the Curlmulti object.
Important: Add_handle does not implicitly add a reference to the Curl object (and thus does not increase the number of references to the Curl object)
Remove_handle (Curl object), None
Corresponds to the Curl_multi_remove_handle () method in Libcurl. This method removes an existing Curl object from the Curlmulti object.
Important: Remove_handle does not implicitly remove references to curl objects (thus reducing the number of references to curl objects).
Fdset () Triple of lists with active file descriptors, readable, writeable, exceptions.
Corresponds to the Curl_multi_fdset () method in Libcurl. This method extracts the file description information from the Curlmulti object. The returned list can be used for select modules to poll for events.
Examples of usage:
Import Pycurl
c = Pycurl. Curl ()
C.setopt (Pycurl. URL, "http://curl.haxx.se")
m = Pycurl. Curlmulti ()
M.add_handle (c)
While 1:
RET, num_handles = M.perform ()
IF ret! = Pycurl. E_call_multi_perform:break
While Num_handles:
Apply (Select.select, M.fdset () + (1,))
While 1:
RET, num_handles = M.perform ()
IF ret! = Pycurl. E_call_multi_perform:breakSelect (timeout), number of ready file descriptors or-1 on timeout
This is a useful function that simplifies the use of combination of fdest () and select modules.
Examples of usage:
Import Pycurl
c = Pycurl. Curl ()
C.setopt (Pycurl. URL, "http://curl.haxx.se")
m = Pycurl. Curlmulti ()
M.add_handle (c)
While 1:
RET, num_handles = M.perform ()
IF ret! = Pycurl. E_call_multi_perform:break
While Num_handles:
ret = M.select (1.0)
if ret = = -1:continue
While 1:
RET, num_handles = M.perform ()
IF ret! = Pycurl. E_call_multi_perform:breakInfo_read ([Max]), Numberof queued messages, a list of successful objects, a list of failed objects
Corresponds to the Curl_multi_info_read () method in Libcurl. This method extracts up to max information from multiple stacks and then returns two lists. The first list contains the operations that completed successfully the second list contains the <curl objects for each failed Curl object. Curl error code, curl error message > sequence.
Curlshare ObjectThe Curlshare object has the following methods:
Setopt (option, value), None
corresponding to the Curl_share_setopt method in Libcurl, option is specified using the curlopt_* constant in Libcurl, but the curlopt_ prefix is now changed to Sh_. Usually value must be Lock_data_ Cookies, or Lock_data_dns.
Examples of usage:
Import Pycurl
Curl = Pycurl. Curl ()
s = Pycurl. Curlshare ()
S.setopt (Pycurl. Sh_share, Pycurl. Lock_data_cookie)
S.setopt (Pycurl. Sh_share, Pycurl. LOCK_DATA_DNS)
Curl.setopt (Pycurl. URL, ' http://curl.haxx.se ')
Curl.setopt (Pycurl. SHARE, s)
Curl.perform ()
Curl.close ()
CallbacksFor better control, Libcurl allows some callback functions to be associated with each connection. In Pycurl, the callback function calls setopt through the Curl object for S writefunction, Readfunction, Headerfunction, Progressfunction, ioctlfunction, or debugfunction these options are set. These options correspond to Libcurl curlopt_* The option to remove the prefix. In Pycurl, the callback function must be a normal Python function, or a method of a class or an extended function type.
Some of the limitations here are that the callback functions for these options can occur at the same time. It allows different callback functions to correspond to different curl objects. More specifically, WriteData's callback function cannot be used for writefunction, The callback function of ReadData cannot be used for Readfunction,writeheader callback function cannot be used for Headerfunction,progressdata callback function cannot be used for progressfunction, The Ioctldata callback function cannot be used for Ioctlfunction,debugdata callback functions that cannot be used in debugfunction. You can overcome this limitation by using an instance method of a class as a callback function and storing the data for each object like a file object with class instance properties.
The signature of each callback function in Pycurl is as follows:
Writefunction (String), number of characters written
Readfunction (number of characters to read), string
Headerfunction (String), number of characters written
Progressfunction (download total, downloaded, upload total, uploaded), status
Debugfunction (Debug message type, debug message string), None
Ioctlfunction (ioctl cmd), status
Example:callbacks for document header and bodyThis example prints the head data to the STDERR print content data to stdout. Also note that neither of them returns the number of bytes written. Writefunction and Headerfunction callback, which returns none when all bytes are written.
# # Callback function invoked when body data was ready
def body (BUF):
# Print body data to stdout
Import Sys
Sys.stdout.write (BUF)
# returning None implies that all bytes were written
# # Callback function invoked when header data are ready
def header (BUF):
# Print Header data to stderr
Import Sys
Sys.stderr.write (BUF)
# returning None implies that all bytes were written
c = Pycurl. Curl ()
C.setopt (Pycurl. URL, "http://www.python.org/")
C.setopt (Pycurl. Writefunction, body)
C.setopt (Pycurl. Headerfunction, header)
C.perform () Example:download/upload Progress callbackThis example shows how to use a progress callback. When you download a document, the uploads parameter will be 0 and vice versa.
# # Callback function invoked when download/upload have progress
def progress (download_t, Download_d, upload_t, upload_d):
Print "Total to download", download_t
Print "Total downloaded", Download_d
Print "Total to upload", upload_t
Print "Total uploaded", upload_d
C.setopt (C.url, "http://slashdot.org/")
C.setopt (c.noprogress, 0)
C.setopt (C.progressfunction, progress)
C.perform () Example:debug callbacksThis example shows how to use Debug callbacks. The Debug information type is an integer indicating type of debug information. The verbose option must be available when this callback is invoked.
def test (Debug_type, debug_msg):
Print "Debug (%d):%s"% (Debug_type, debug_msg)
c = Pycurl. Curl ()
C.setopt (Pycurl. URL, "http://curl.haxx.se/")
C.setopt (Pycurl. VERBOSE, 1)
C.setopt (Pycurl. Debugfunction, test)
C.perform ()
Other examplesPycrul also contains test scripts and cases that demonstrate how to use different callbacks in Libcurl. For example, the file examples/file_upload.py contains the case code for how to use Readfunction, ' Tests/test_ cb.py ' demo writefunction and Headerfunction, ' tests/test_debug.py ' demo debugfunction, ' tests/test_getinfo.py ' Demo Progressfunction.
============================================================================
Edit text saved under C:/python25/lib pycurl_test.py
Import Pycurl
Import Stringi
def geturlcontent_pycurl (URL):
c = Pycurl. Curl ()
C.setopt (Pycurl. Url,url)
b = Stringio.stringio ()
C.setopt (Pycurl. Writefunction, B.write)
C.setopt (Pycurl. Followlocation, 1)
C.setopt (Pycurl. Maxredirs, 5)
#c. setopt (Pycurl. PROXY, ' http://11.11.11.11:8080 ')
#c. setopt (Pycurl. Proxyuserpwd, ' aaa:aaa ')
C.perform ()
Return B.getvalue ()
Enter import pycurl_test on the command line
url = ' Http://blog.csdn.net '
Content = Geturlcontent_pycurl (URL)
Print content
=============================================================