Skip to content

Instantly share code, notes, and snippets.

@maczniak
Created April 23, 2020 04:25
Show Gist options
  • Save maczniak/db34452555f33805302d2c5557167164 to your computer and use it in GitHub Desktop.
Save maczniak/db34452555f33805302d2c5557167164 to your computer and use it in GitHub Desktop.
Python3 urllib.request debug (request and response details)
import urllib.request
# debug setting
http_handler = urllib.request.HTTPHandler(debuglevel=1)
try:
import ssl
https_handler = urllib.request.HTTPSHandler(debuglevel=1)
opener = urllib.request.build_opener(http_handler, https_handler)
except ImportError:
opener = urllib.request.build_opener(http_handler)
urllib.request.install_opener(opener)
# ...
urllib.request.urlopen('https://google.com/')
# sample output
"""
send: b'GET / HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: google.com\r\nUser-Agent: Python-urllib/3.7\r\nConnection: close\r\n\r\n'
reply: 'HTTP/1.1 301 Moved Permanently\r\n'
header: Location: https://www.google.com/
header: Content-Type: text/html; charset=UTF-8
header: Date: Thu, 23 Apr 2020 04:23:54 GMT
header: Expires: Sat, 23 May 2020 04:23:54 GMT
header: Cache-Control: public, max-age=2592000
header: Server: gws
header: Content-Length: 220
header: X-XSS-Protection: 0
header: X-Frame-Options: SAMEORIGIN
header: Alt-Svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,h3-T050=":443"; ma=2592000
header: Connection: close
send: b'GET / HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: www.google.com\r\nUser-Agent: Python-urllib/3.7\r\nConnection: close\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Thu, 23 Apr 2020 04:23:54 GMT
header: Expires: -1
header: Cache-Control: private, max-age=0
header: Content-Type: text/html; charset=ISO-8859-1
header: P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
header: Server: gws
header: X-XSS-Protection: 0
header: X-Frame-Options: SAMEORIGIN
header: Set-Cookie: 1P_JAR=2020-04-23-04; expires=Sat, 23-May-2020 04:23:54 GMT; path=/; domain=.google.com; Secure
header: Set-Cookie: 1P_JAR=2020-04-23-04; expires=Sat, 23-May-2020 04:23:54 GMT; path=/; domain=.google.com; Secure
header: Alt-Svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,h3-T050=":443"; ma=2592000
header: Accept-Ranges: none
header: Vary: Accept-Encoding
header: Connection: close
header: Transfer-Encoding: chunked
"""
# if you use requests package, please refer to the below.
# https://requests.readthedocs.io/en/master/api/#api-changes
# https://stackoverflow.com/questions/16337511/log-all-requests-from-the-python-requests-module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment