python3.5版設定request的不會顯示debug log的解法


有人問為何3.4版就可以顯示http request的log
import http.client
http.client.HTTPConnection.debuglevel = 1
這裡看起來是設定debug level有問題,實測3.5版,3.6版都有受影響

平常

import urllib.request
import http.client
http.client.HTTPSConnection.debuglevel = 1
response = urllib.request.urlopen('https://www.google.com')
print('status:',response.status)

解法

import urllib.request
handler = urllib.request.HTTPSHandler(debuglevel=1)
opener = urllib.request.build_opener(handler)
response = opener.open('https://www.google.com')
print('status:',response.status)


參考:
https://stackoverflow.com/questions/42876579

留言