Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the header with pycurl

Tags:

How do I read the response headers returned from a PyCurl request?

like image 801
sverrejoh Avatar asked Jan 23 '09 07:01

sverrejoh


People also ask

What is PycURL used for?

PycURL is a Python interface to libcurl, the multiprotocol file transfer library. Similarly to the urllib Python module, PycURL can be used to fetch objects identified by a URL from a Python program.

Can I use cURL with Python?

In Python, cURL transfers requests and data to and from servers using PycURL. PycURL functions as an interface for the libcURL library within Python. Almost every programming language can use REST APIs to access an endpoint hosted on a web server.

How do you use curl post in Python?

To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.


1 Answers

There are several solutions (by default, they are dropped). Here is an example using the option HEADERFUNCTION which lets you indicate a function to handle them.

Other solutions are options WRITEHEADER (not compatible with WRITEFUNCTION) or setting HEADER to True so that they are transmitted with the body.

#!/usr/bin/python  import pycurl import sys  class Storage:     def __init__(self):         self.contents = ''         self.line = 0      def store(self, buf):         self.line = self.line + 1         self.contents = "%s%i: %s" % (self.contents, self.line, buf)      def __str__(self):         return self.contents  retrieved_body = Storage() retrieved_headers = Storage() c = pycurl.Curl() c.setopt(c.URL, 'http://www.demaziere.fr/eve/') c.setopt(c.WRITEFUNCTION, retrieved_body.store) c.setopt(c.HEADERFUNCTION, retrieved_headers.store) c.perform() c.close() print retrieved_headers print retrieved_body 
like image 54
bortzmeyer Avatar answered Oct 23 '22 01:10

bortzmeyer