Python + request get a response (elapsed) and response time (timeout) "Seven"

When requests issued request, the response time of the interface, a point is we need to focus, if the response time is too long, it is unreasonable.
If the server did not respond in a timely manner, we can not have been waiting for, you can set a timeout timeout

Specific implementation is as follows:

  Timeout (default unit: s):

        timeout = 0.5: to set the timeout time of less than 0.5s

            timeout = (0.5,0.8): Wait Time Setting Section

    When a timeout occurs, it will throw this exception: requests.exceptions.ConnectTimeout: HTTPConnectionPool    

  Gets Response time:

      r.elapsed.total_seconds()  

       

#-*- coding:utf-8 -*-
import requests
from requests import exceptions

try:
    #timeout=0.1 ,timeout=(0.5,0.8)
    r = requests.post(url=url, data=data, headers=headers, verify=False, timeout=0.5)

   r.elapsed.total_seconds() #获取实际的响应时间
   print r.json()
the except exceptions.Timeout AS E: 

  Print ( "thrown")

 

Guess you like

Origin www.cnblogs.com/syw20170419/p/10951237.html