Why can't I get Python's urlopen() method to work on Windows?

前端 未结 4 1485
一生所求
一生所求 2020-12-04 02:16

Why isn\'t this simple Python code working?

import urllib
file = urllib.urlopen(\'http://www.google.com\')
print file.read()

This is the er

4条回答
  •  星月不相逢
    2020-12-04 02:51

    Try using urllib2 if it is feasible to change some lines of code. Set the timeout argument in seconds

    For example:

    urllib2.urlopen(http://www.abc.com/api, timeout=20)
    

    Here the connection persists for a longer duration. So if for example you are reading an XML file that is too large it avoids incomplete reading.

    The above code will never work if the Net connection is slow or it breaks suddenly.

提交回复
热议问题