How to debug urllib2 request that uses a basic authentication handler

前端 未结 1 1805
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 20:03

I\'m making a request using urllib2 and the HTTPBasicAuthHandler like so:

import urllib2

theurl = \'http://someurl.com\'
username          


        
相关标签:
1条回答
  • 2020-12-15 20:31

    Have you tried setting the debug level in your own HTTP handler? Change your code to something like this:

    >>> import urllib2
    >>> handler=urllib2.HTTPHandler(debuglevel=1)
    >>> opener = urllib2.build_opener(handler)
    >>> urllib2.install_opener(opener)
    >>> resp=urllib2.urlopen('http://www.google.com').read()
    send: 'GET / HTTP/1.1
          Accept-Encoding: identity
          Host: www.google.com
          Connection: close
          User-Agent: Python-urllib/2.7'
    reply: 'HTTP/1.1 200 OK'
    header: Date: Sat, 08 Oct 2011 17:25:52 GMT
    header: Expires: -1
    header: Cache-Control: private, max-age=0
    header: Content-Type: text/html; charset=ISO-8859-1
    ... the remainder of the send / reply other than the data itself 
    

    So the three lines to prepend are:

    handler=urllib2.HTTPHandler(debuglevel=1)
    opener = urllib2.build_opener(handler)
    urllib2.install_opener(opener)
    ... the rest of your urllib2 code...
    

    That will show the raw HTTP send / reply cycle on stderr.

    Edit from comment

    Does this work?

    ... same code as above this line
    opener=urllib2.build_opener(authhandler, urllib2.HTTPHandler(debuglevel=1))
    ... rest of your code
    
    0 讨论(0)
提交回复
热议问题