Python requests inside a Django view hangs out and needs timeout

时间:2019-04-23 15:06:28

标签: python django django-views python-requests timeout

I'm seeking help for this rather strange behaviour. I have a Django view that gets called after a button click in a Django template

@require_http_methods(['GET', 'POST'])
@login_required
@transaction.atomic
def create_key(request, slug):

    #some unrelated code

    try:
        r = requests.post(
            some_url,
            data={
                #some_data
            },
            auth=(client_id, client_secret),
            timeout=(req_to, res_to)
        )
        if r.status_code == 200:
            return True
        else:
            return False
    except ReadTimeout as to:
        # handle exception
        return True
    except Exception as e:
        # handle exception
        return False

    #some unrelated code

that basically calls an API endpoint to create a key. Now the request with Postman works fine, taking out that python snippet and running it alone also works, but when put inside this Django view it hangs until reaches response timeout.

Does anybody has any idea or pointer on where the problem might be?

Thank you in advance!

EDIT: i've found similar problems but these while they share with me the same structure, the problem was somewhere else

Why Python requests library failing to get response?

LiveServerTestCase hangs at python-requests post call in django view

0 个答案:

没有答案