本文介绍了如何用pycurl读取标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

有几种解决方案被丢弃)。这是一个
示例,使用选项HEADERFUNCTION,它允许您指示一个
函数来处理它们。

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

其他解决方案是选项WRITEHEADER
WRITEFUNCTION)或将HEADER设置为True,以便它们与正文传送

Other solutions are options WRITEHEADER (not compatible withWRITEFUNCTION) or setting HEADER to True so that they are transmittedwith 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

这篇关于如何用pycurl读取标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 07:03