[Python] Turning on debug output for python 3 urllib

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    In python 2, it was possible to get debug output from urllib by doing

    import httplib
    import urllib
    httplib.HTTPConnection.debuglevel = 1
    response = urllib.urlopen('http://example.com').read()


    However, in python 3 it looks like this has been moved to

    http.client.HTTPConnection.set_debuglevel(level)


    However, I'm using urllib not http.client directly. How can I set it up so that my http request display debugging information in this way?

    Here's what I"m using so far. What's the best way to proceed if I want to be able to get debug information?

    #Request Login page
    cookiejar = http.cookiejar.CookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookiejar))
    request = urllib.request.Request(options.uri)
    add_std_headers(request)
    response = opener.open(request)
    response_string = response.read().decode("utf8")
    # ...

    Continue reading...

Compartilhe esta Página