15

我有一个使用 urllib2 的 Python Web 客户端。将 HTTP 标头添加到我的传出请求中很容易。我只是创建一个我想要添加的标题的字典,并将它传递给 Request 初始化程序。

但是,其他“标准”HTTP 标头以及我明确添加的自定义标头都会添加到请求中。当我使用 Wireshark 嗅探请求时,除了我自己添加的标题之外,我还看到了标题。我的问题是如何访问这些标头?我想记录每个请求(包括全套HTTP 标头),但不知道如何。

任何指针?

简而言之:如何从 urllib2 创建的 HTTP 请求中获取所有传出标头?

4

8 回答 8

10

如果您想查看发送的文字 HTTP 请求,并因此查看每个最后一个标头与线路上表示的完全相同,那么您可以告诉urllib2使用您自己的HTTPHandler打印输出(或保存,或其他)版本传出的 HTTP 请求。

import httplib, urllib2

class MyHTTPConnection(httplib.HTTPConnection):
    def send(self, s):
        print s  # or save them, or whatever!
        httplib.HTTPConnection.send(self, s)

class MyHTTPHandler(urllib2.HTTPHandler):
    def http_open(self, req):
        return self.do_open(MyHTTPConnection, req)

opener = urllib2.build_opener(MyHTTPHandler)
response = opener.open('http://www.google.com/')

运行这段代码的结果是:

GET / HTTP/1.1
Accept-Encoding: identity
Host: www.google.com
Connection: close
User-Agent: Python-urllib/2.6
于 2010-10-27T14:36:54.197 回答
5

urllib2 库使用 OpenerDirector 对象来处理实际打开。幸运的是,python 库提供了默认值,因此您不必这样做。然而,正是这些 OpenerDirector 对象添加了额外的标头。

在发送请求后查看它们是什么(例如,以便您可以记录它):

req = urllib2.Request(url='http://google.com')
response = urllib2.urlopen(req)
print req.unredirected_hdrs

(produces {'Host': 'google.com', 'User-agent': 'Python-urllib/2.5'} etc)

unredirected_hdrs 是 OpenerDirectors 转储其额外标头的地方。只需查看req.headers将仅显示您自己的标题 - 库为您保留那些不受干扰的标题。

如果您需要在发送请求之前查看标头,则需要子类化 OpenerDirector 以拦截传输。

希望有帮助。

编辑:我忘了提到,一旦发送请求,req.header_items()将为您提供所有标头的元组列表,包括您自己的和 OpenerDirector 添加的。我应该首先提到这一点,因为它是最直接的 :-) 抱歉。

编辑2:在您对定义自己的处理程序的示例提出问题之后,这是我想出的示例。任何对请求链的关注是我们需要确保处理程序对于多个请求是安全的,这就是为什么我不喜欢直接替换 HTTPConnection 类上 putheader 的定义。

遗憾的是,由于 HTTPConnection 和 AbstractHTTPHandler 的内部结构非常内部,我们必须从 python 库中复制大部分代码来注入我们的自定义行为。假设我没有在下面犯错并且这与我在 5 分钟的测试中一样有效,如果您将 Python 版本更新为修订号(即:2.5.x 到 2.5.y 或2.5 到 2.6 等)。

因此,我应该提到我使用的是 Python 2.5.1。如果您有 2.6 或特别是 3.0,您可能需要相应地进行调整。

如果这不起作用,请告诉我。我对这个问题太感兴趣了:

import urllib2
import httplib
import socket


class CustomHTTPConnection(httplib.HTTPConnection):

    def __init__(self, *args, **kwargs):
        httplib.HTTPConnection.__init__(self, *args, **kwargs)
        self.stored_headers = []

    def putheader(self, header, value):
        self.stored_headers.append((header, value))
        httplib.HTTPConnection.putheader(self, header, value)


class HTTPCaptureHeaderHandler(urllib2.AbstractHTTPHandler):

    def http_open(self, req):
        return self.do_open(CustomHTTPConnection, req)

    http_request = urllib2.AbstractHTTPHandler.do_request_

    def do_open(self, http_class, req):
        # All code here lifted directly from the python library
        host = req.get_host()
        if not host:
            raise URLError('no host given')

        h = http_class(host) # will parse host:port
        h.set_debuglevel(self._debuglevel)

        headers = dict(req.headers)
        headers.update(req.unredirected_hdrs)
        headers["Connection"] = "close"
        headers = dict(
            (name.title(), val) for name, val in headers.items())
        try:
            h.request(req.get_method(), req.get_selector(), req.data, headers)
            r = h.getresponse()
        except socket.error, err: # XXX what error?
            raise urllib2.URLError(err)
        r.recv = r.read
        fp = socket._fileobject(r, close=True)

        resp = urllib2.addinfourl(fp, r.msg, req.get_full_url())
        resp.code = r.status
        resp.msg = r.reason

        # This is the line we're adding
        req.all_sent_headers = h.stored_headers
        return resp

my_handler = HTTPCaptureHeaderHandler()
opener = urllib2.OpenerDirector()
opener.add_handler(my_handler)
req = urllib2.Request(url='http://www.google.com')

resp = opener.open(req)

print req.all_sent_headers

shows: [('Accept-Encoding', 'identity'), ('Host', 'www.google.com'), ('Connection', 'close'), ('User-Agent', 'Python-urllib/2.5')]
于 2009-03-02T20:57:21.153 回答
2

像这样的东西怎么样:

import urllib2
import httplib

old_putheader = httplib.HTTPConnection.putheader
def putheader(self, header, value):
    print header, value
    old_putheader(self, header, value)
httplib.HTTPConnection.putheader = putheader

urllib2.urlopen('http://www.google.com')
于 2009-03-02T20:58:53.847 回答
2

低级解决方案:

import httplib

class HTTPConnection2(httplib.HTTPConnection):
    def __init__(self, *args, **kwargs):
        httplib.HTTPConnection.__init__(self, *args, **kwargs)
        self._request_headers = []
        self._request_header = None

    def putheader(self, header, value):
        self._request_headers.append((header, value))
        httplib.HTTPConnection.putheader(self, header, value)

    def send(self, s):
        self._request_header = s
        httplib.HTTPConnection.send(self, s)

    def getresponse(self, *args, **kwargs):
        response = httplib.HTTPConnection.getresponse(self, *args, **kwargs)
        response.request_headers = self._request_headers
        response.request_header = self._request_header
        return response

例子:

conn = HTTPConnection2("www.python.org")
conn.request("GET", "/index.html", headers={
    "User-agent": "test",
    "Referer": "/",
})
response = conn.getresponse()

response.status,response.reason:

1: 200 OK

response.request_headers:

[('Host', 'www.python.org'), ('Accept-Encoding', 'identity'), ('Referer', '/'), ('User-agent', 'test')]

response.request_header:

GET /index.html HTTP/1.1
Host: www.python.org
Accept-Encoding: identity
Referer: /
User-agent: test
于 2011-08-09T10:09:50.703 回答
2

另一个解决方案,女巫使用了How do you get default headers in a urllib2 Request? 但不从 std-lib 复制代码:

class HTTPConnection2(httplib.HTTPConnection):
    """
    Like httplib.HTTPConnection but stores the request headers.
    Used in HTTPConnection3(), see below.
    """
    def __init__(self, *args, **kwargs):
        httplib.HTTPConnection.__init__(self, *args, **kwargs)
        self.request_headers = []
        self.request_header = ""

    def putheader(self, header, value):
        self.request_headers.append((header, value))
        httplib.HTTPConnection.putheader(self, header, value)

    def send(self, s):
        self.request_header = s
        httplib.HTTPConnection.send(self, s)


class HTTPConnection3(object):
    """
    Wrapper around HTTPConnection2
    Used in HTTPHandler2(), see below.
    """
    def __call__(self, *args, **kwargs):
        """
        instance made in urllib2.HTTPHandler.do_open()
        """
        self._conn = HTTPConnection2(*args, **kwargs)
        self.request_headers = self._conn.request_headers
        self.request_header = self._conn.request_header
        return self

    def __getattribute__(self, name):
        """
        Redirect attribute access to the local HTTPConnection() instance.
        """
        if name == "_conn":
            return object.__getattribute__(self, name)
        else:
            return getattr(self._conn, name)


class HTTPHandler2(urllib2.HTTPHandler):
    """
    A HTTPHandler which stores the request headers.
    Used HTTPConnection3, see above.

    >>> opener = urllib2.build_opener(HTTPHandler2)
    >>> opener.addheaders = [("User-agent", "Python test")]
    >>> response = opener.open('http://www.python.org/')

    Get the request headers as a list build with HTTPConnection.putheader():
    >>> response.request_headers
    [('Accept-Encoding', 'identity'), ('Host', 'www.python.org'), ('Connection', 'close'), ('User-Agent', 'Python test')]

    >>> response.request_header
    'GET / HTTP/1.1\\r\\nAccept-Encoding: identity\\r\\nHost: www.python.org\\r\\nConnection: close\\r\\nUser-Agent: Python test\\r\\n\\r\\n'
    """
    def http_open(self, req):
        conn_instance = HTTPConnection3()
        response = self.do_open(conn_instance, req)
        response.request_headers = conn_instance.request_headers
        response.request_header = conn_instance.request_header
        return response

编辑:更新源

于 2011-08-09T13:16:27.890 回答
0

参见 urllib2.py:do_request (line 1044 (1067)) 和 urllib2.py:do_open (line 1073) (line 293) self.addheaders = [('User-agent', client_version)] (只添加了'User-agent' )

于 2009-03-02T20:46:02.037 回答
0

在我看来,您正在寻找响应对象的标头,其中包括Connection: close等。这些标头位于 urlopen 返回的对象中。获取它们很容易:

from urllib2 import urlopen
req = urlopen("http://www.google.com")
print req.headers.headers

req.headershttplib.HTTPMessage的一个实例

于 2010-10-27T14:49:24.677 回答
-1

它应该将默认的 http 标头(由w3.org指定)与您指定的标头一起发送。如果您想完整查看它们,可以使用WireShark 之类的工具。

编辑:

如果您想记录它们,您可以使用WinPcap捕获特定应用程序(在您的情况下为 python)发送的数据包。您还可以指定数据包的类型和许多其他详细信息。

-约翰

于 2009-03-02T20:48:15.697 回答