Python Forum

Full Version: Can't read url when specifying more than a specific number of bytes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This works

from urllib.request import urlopen

data = urlopen("https://cinnamon-spices.linuxmint.com/json/applets.json").read(60000)

print(data)
This hangs

from urllib.request import urlopen

data = urlopen("https://cinnamon-spices.linuxmint.com/json/applets.json").read(70000)

print(data)
or without a specific number of bytes, it hangs too


from urllib.request import urlopen

data = urlopen("https://cinnamon-spices.linuxmint.com/json/applets.json").read()

print(data)
It only happens for this specific site (that I know of).

Any ideas on what might be going on? I'm totally new to python.
It do not hang for me.
Anyway should not use urllib,use Requests
Requests has build in JSON decoder.
import requests

response = requests.get("https://cinnamon-spices.linuxmint.com/json/applets.json")
data = response.json()
print(data)
Example parse:
>>> data['location-detection@heimdall']
{'author_id': '0',
 'author_user': 'none',
 'created': 1408356497,
 'description': 'Shows the geographic location of your public IP and '
                'country-flag.',
 'file': '/files/applets/[email protected]',
 'icon': '/files/applets/[email protected]',
 'last_edited': 1504832706,
 'name': 'Location Detection + Flags',
 'score': 2,
 'screenshot': '/git/applets/location-detection@heimdall/screenshot.png',
 'spices-id': 196,
 'uuid': 'location-detection@heimdall'}

>>> data['location-detection@heimdall']['screenshot']
'/git/applets/location-detection@heimdall/screenshot.png'
Thank you @snippsat!

Using requests did the trick and worked just fine. My program was only a minimal reproduction of a problem I'm having with one library. Have a look at https://github.com/linuxmint/Cinnamon/is...-411201291 and the following messages.

Knowing that this works with requests is useful. It sounds like something where the standard library could do better so I guess it might be worth reporting to python?
Just to report this theads "resolution". I ended up reporting to python here, and the culprit was the "Connection: close" header added by the standard library by default. The best solution for now (other than switching to the requests library) seems to use the underlying "http.client" directly so I can customize the headers sent and avoid sending the "Connection: close" one. Although apparently it should just work with the default headers and the issue is due to some server misconfiguration.