[Python] Python script hangs on requests.get inside a loop

Discussão em 'Python' iniciado por Stack, Julho 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have a script that fetches data from a REST API using requests.get inside a for loop. Sometimes, the script hangs or takes a long time to proceed. What could be causing this? How can I ensure my script does not hang indefinitely when making frequent HTTP requests? I have tried adding timeouts, but I am not sure I’m implementing them correctly.

    import requests
    urls = ['http://example.com/1', 'http://example.com/2']
    for url in urls:
    response = requests.get(url)
    print(response.text)


    Any advice on best practices for handling this scenario?

    I tried adding a timeout parameter to requests.get, like this:

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

    Continue reading...

Compartilhe esta Página