iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 19
0
自我挑戰組

Why it works: python requests and urllib3系列 第 19

Day19-Requests-The User Guide-Advanced Usage-4

Requests也有提供Hook系統,目前僅針對response,可以使用hooks parameter並傳入key為hook name;value為callback function的dictionary。關於callback function的部分,有沒有回傳值都可以。

def print_url(r, *args, **kwargs):
    print(r.url)

requests.get('https://httpbin.org/', hooks={'response': print_url})

Requests支援的HTTP Verbs如下: GET, OPTIONS, HEAD, POST, PUT, PATCH and DELETE
此外,也可以使用客製化的Verb

Requests預設使用Blocking IO的模式,亦即需要等到整個response都被下載完成才會繼續往下做事,若要使用Non-Blocking IO的話可以參考

為了避免請求被回應過慢的Server給卡住,嚴重的話可能被卡的時間是以分鐘計算的,使用上必須要記得設定timeout

  • connect timeout
    • the number of seconds Requests will wait for your client to establish a connection to a remote machine
  • read timeout
    • the number of seconds the client will wait for the server to send a response.

參考


上一篇
Day18-Requests-The User Guide-Advanced Usage-3
下一篇
Day20-urllib3-User Guide-1
系列文
Why it works: python requests and urllib330
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言