如何使用pycurl讀取標頭

[英]How to read the header with pycurl


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

如何讀取PyCurl請求返回的響應頭?

4 个解决方案

#1


28  

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.

有幾種解決方案(默認情況下,它們被刪除)。下面是使用選項HEADERFUNCTION的示例,它允許您指示處理它們的函數。

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

其他解決方案是選項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

#2


9  

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()

Add any other curl setopts as necessary/desired, such as FOLLOWLOCATION.

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

#3


6  

Anothr alternate, human_curl usage: pip human_curl

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': '*'}

#4


1  

This might or might not be an alternative for you:

這可能是也可能不是您的替代選擇:

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/01/23/f3a62d242ce0f2cdc56d8da6892115c6.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com