Why is this requests get not working with this url

Question:

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.

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: [email protected]
License: Apache 2.0
Location: /usr/lib/python3/dist-packages
Requires:
Required-by:
Asked By: perperam

||

Answers:

It works perfectly for me. If it ‘hangs’ a poor connection can cause it. What you can do is provide a timeout argument, which will wait that many seconds, and if it does not receive a response from the server will continue executing code.

import requests
url = "https://stackoverflow.com" 
r = requests.get(url, timeout=2)
Answered By: user16965639

It works for me. However if you still have problems with requests, you could even use urllib.request

For example:

import urllib.request
import requests

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

r = requests.get(url)
print(r)
u = urllib.request.urlopen(url).getcode()
print(u)

They have the same output:

<Response [200]>
200
Answered By: Edoardo Balducci

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())
Answered By: asymptote
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.