Urllib2.urlopen will hang forever, despite the timeout

Hope this is a pretty simple question, but it drives me crazy. I am using Python 2.7.3 when installing ubuntu 12.10 server out of the box. I continued to scale the problem until I got to this snippet:

import urllib2 x=urllib2.urlopen("http://casacinema.eu/movie-film-Matrix+trilogy+123+streaming-6165.html", timeout=5) 

He just hangs forever, never goes into timeout. I am obviously doing something wrong. Can anybody help? Thank you very much!

Matteo

+4
source share
2 answers

It looks like you are having a problem with a proxy server. Here's a great explanation on how to get around this: Attempting to access the Internet using urllib2 in Python .

I executed my code on my ubuntu using python 2.7.3 and did not see any errors.

Also consider using requests :

 import requests response = requests.get("http://casacinema.eu/movie-film-Matrix+trilogy+123+streaming-6165.html", timeout=5) print response.status_code 

See also:

+3
source

The original poster stated that they do not understand why it hangs, but they also wanted urllib.request.urlopen not to hang. I can’t say how to keep him from hanging, but if it helps someone, then why can he hang.

The Python-urllib/3.6 client Python-urllib/3.6 picky. He expects, for example, that the server will return HTTP/1.1 200 OK not HTTP 200 OK . It also expects the server to close the connection when it sends connection: close to the headers.

The best way to diagnose this is to get the original result of the server’s response and compare it with another server’s response, which, as you know, works. Then, if you must create a server and process the response to determine exactly what the difference is. Perhaps this can lead to at least a change on the server and allow it to not hang.

0
source

Source: https://habr.com/ru/post/1482959/


All Articles