repeat requests if it takes more than 10 seconds

Question:

I wrote this script:

#Defining Functions to use in the script
def make_request(student_id):


    """
    Makes a response for the student ID given, Keeps repeating it till it's a successful response.

    """

    url = 'http://app1.helwan.edu.eg/Commerce/HasasnUpMlist.asp' #Base URL to our college website

    params = {
        'z_dep': '=',
        'z_st_name': 'LIKE',
        'z_st_settingno': '=',
        'x_st_settingno': f'{student_id}',
        'x_st_name': '',
        'z_gro': '=',
        'x_gro': '',
        'x_dep': '',
        'z_sec': 'LIKE',
        'x_sec': '',
        'Submit': '++++حفظ++++'
    }


    
    response = requests.get(url, params=params) #Make a request for the current student

    response_state =  response.status_code

    while response_state != 200 :

        response = requests.get(url,params= params)
        response_state = response.status_code
        
    return response

I want to adjust it so that it repeats the request if it took more than 10 seconds.
I tried doing it with time.time before and after in the body of the while loop but no luck, or maybe I performed it poorly due to limited knowledge of execution order.
I’d appreciate your help.

Asked By: Esmael Maher

||

Answers:

To do so you can use the timeout option directly. So
in the requests.get you can pass an additional option timeout=10 to make it raise an error after 10 seconds
if it does not recieve anything. This has to be wrapped in a
try and except block to work.

In short, change the line

response = requests.get(url, params=params, timeout=10)

to

while True:
    try:
        response = request.get(url, params=params, timeout=10)
        break
    except requests.exceptions.ReadTimeout, requests.exceptions.ConnectTimeout:
        continue

In short your code will turn to this:

#Defining Functions to use in the script
def make_request(student_id):


    """
    Makes a response for the student ID given, Keeps repeating it till it's a successful response.

    """

    url = 'http://app1.helwan.edu.eg/Commerce/HasasnUpMlist.asp' #Base URL to our college website

    params = {
        'z_dep': '=',
        'z_st_name': 'LIKE',
        'z_st_settingno': '=',
        'x_st_settingno': f'{student_id}',
        'x_st_name': '',
        'z_gro': '=',
        'x_gro': '',
        'x_dep': '',
        'z_sec': 'LIKE',
        'x_sec': '',
        'Submit': '++++حفظ++++'
    }

    # I removed this request to reduce complications
    response_state = 0

    while response_state != 200 :
        try:
            response = requests.get(url,params= params, timeout=10)
        except requests.execeptions.ReadTimeout, requests.exceptions.ConnectTimeout:
            continue
        response_state = response.status_code
        
    return response
Answered By: AmaanK
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.