Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is this requests get not working with this url

If i run this Python code my program just hangs up. (I don`t get any error.)

import requests
url = "https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/generative/dcgan.ipynb"
r = requests.get(url)

But this works perfectly fine as expected.

import requests
url = "https://stackoverflow.com" 
r = requests.get(url)

Using curl to get the github file worked also fine.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

curl https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/generative/dcgan.ipynb

So can you reproduce my problem, did i overlocked something trivial or is it just something with my python environment?

python3 --version
Python 3.8.10

pip show requests
Name: requests
Version: 2.22.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: /usr/lib/python3/dist-packages
Requires:
Required-by:

>Solution :

You may be getting stuck in redirects. You can overcome this by setting allow_redirects=False. The following code works for me:

import requests
url = "https://raw.githubusercontent.com/tensorflow/docs/master/site/en/tutorials/generative/dcgan.ipynb"

try: 
    r = requests.get(url, allow_redirects=False, timeout=10)
except requests.exceptions.Timeout as err: 
    print(err)

print(r.json())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading