如何使用pycurl读取标头

时间:2009-01-23 07:21:48

标签: python curl pycurl

如何读取PyCurl请求返回的响应头?

4 个答案:

答案 0 :(得分:29)

有几种解决方案(默认情况下,它们被删除)。这是一个 使用选项HEADERFUNCTION的示例,它允许您指示a 处理它们的功能。

其他解决方案是WRITEHEADER选项(不兼容 WRITEFUNCTION)或将HEADER设置为True以便传输它们 与身体。

#!/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

答案 1 :(得分:11)

import pycurl
from StringIO import StringIO

headers = StringIO()

c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.HEADER, 1)
c.setopt(c.NOBODY, 1) # header only, no body
c.setopt(c.HEADERFUNCTION, headers.write)

c.perform()

print headers.getvalue()

根据需要/期望添加任何其他卷曲setopts,例如FOLLOWLOCATION。

答案 2 :(得分:6)

Anothr alternate,human_curl用法:pip human_curl

In [1]: import human_curl as hurl

In [2]: r = hurl.get("http://stackoverflow.com")

In [3]: r.headers
Out[3]: 
{'cache-control': 'public, max-age=45',
 'content-length': '198515',
 'content-type': 'text/html; charset=utf-8',
 'date': 'Thu, 01 Sep 2011 11:53:43 GMT',
 'expires': 'Thu, 01 Sep 2011 11:54:28 GMT',
 'last-modified': 'Thu, 01 Sep 2011 11:53:28 GMT',
 'vary': '*'}

答案 3 :(得分:1)

这可能是也可能不适合您:

import urllib
headers = urllib.urlopen('http://www.pythonchallenge.com').headers.headers