开发者

python httplib: getting the outgoing request headers

开发者 https://www.devze.com 2023-01-04 19:11 出处:网络
I do: con = HTTPConnection(SERVER_NAME) con.request(\'GET\', PATH, HEADERS) resp = con.getresponse() For debugging reasons, I want to see the request I used (it\'s fields, path, method,..). I开发者

I do:

con = HTTPConnection(SERVER_NAME)
con.request('GET', PATH, HEADERS)
resp = con.getresponse()

For debugging reasons, I want to see the request I used (it's fields, path, method,..). I开发者_StackOverflow社区 would expect there to be some sort of con.getRequest() or something of the sort but didn't find anything. Ideas?


Try

con.setdebuglevel(1)

That will enable debugging output, which among other things, will print out all the data it sends.

If you only want to get the headers and request line, not the request body (or any other debugging output), you can subclass HTTPConnection and override the _output method, which is called by the class itself to produce output (except for the request body). You'd want to do something like this:

class MyHTTPConnection(HTTPConnection):
    def _output(self, s):
        print repr(s)
        super(MyHTTPConnection, self)._output(s)

For more details on how that works and possible alternatives, have a look at the httplib source code.

0

精彩评论

暂无评论...
验证码 换一张
取 消