2014-11-03 2 views
1

프록시를 통과하는 Python의 urllib2로 HTTP 요청을 만들고 있습니다.프록시를 통해 urllib2 요청을 디버깅하는 방법

proxy_handler = urllib2.ProxyHandler({'http': 'http://myproxy'}) 
opener = urllib2.build_opener(proxy_handler) 
urllib2.install_opener(opener) 
r = urllib2.urlopen('http://www.pbr.com') 

이 요청에서 모든 헤더를 기록하고 싶습니다. 나는 표준을 사용하는 것은 당신이 할 수 HTTPHandler 것을 알고 : ProxyHandler이 같은 일이

handler = urllib2.HTTPHandler(debuglevel=1) 

있습니까?

답변

2

저는 debuglevel이 문서화되지 않았 음을 확신합니다. 실제로

, 그냥 앞으로 함께 편의를 위해 실제로 httpliburllib2 그 기능, 그래서 당신은 당신의 HTTP 개체 공장 등의 기본 httplib.HTTPConnection 대신 lambda: httplib.HTTPConnection(debuglevel=1)을 통과 할 필요가 없습니다. 따라서 다른 핸들러에서 비슷한 것을 찾지 못할 수도 있습니다.

하지만 구현의 문서화되지 않은 기능에 의존하려면 직접보고 싶은 read the source이 필요합니다. 여하튼


, 핸들러의에 디버깅을 추가 할 수있는 확실한 방법은 그들을 서브 클래스 화해, 스스로를하는 것입니다. 예를 들어 :

class LoggingProxyHandler(urllib2.ProxyHandler): 
    def proxy_open(self, req, proxy, type): 
     had_proxy = req.has_proxy() 
     response = super(LoggingProxyHandler, self).proxy_open(req, proxy, type) 
     if not had_proxy and req.has_proxy(): 
      # log stuff here 
     return response 

나는 내부 지식에 의존하고있어 그 ProxyHandler 통화 요청에 set_proxy가 하나가 하나를 필요로하지 않는 경우. 대신 응답을 조사하는 것이 더 깔끔할 수 있지만 원하는 방식으로 모든 정보를 얻지 못할 수도 있습니다.

관련 문제