为什么我不能让 Python 的 urlopen() 方法在 Windows 上工作? [英] Why can't I get Python's urlopen() method to work on Windows?

查看:26
本文介绍了为什么我不能让 Python 的 urlopen() 方法在 Windows 上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个简单的 Python 代码不起作用?

Why isn't this simple Python code working?

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

这是我得到的错误:

Traceback (most recent call last):
  File "C:\workspace\GarchUpdate\src\Practice.py", line 26, in <module>
    file = urllib.urlopen('http://www.google.com')
  File "C:\Python26\lib\urllib.py", line 87, in urlopen
    return opener.open(url)
  File "C:\Python26\lib\urllib.py", line 206, in open
    return getattr(self, name)(url)
  File "C:\Python26\lib\urllib.py", line 345, in open_http
    h.endheaders()
  File "C:\Python26\lib\httplib.py", line 892, in endheaders
    self._send_output()
  File "C:\Python26\lib\httplib.py", line 764, in _send_output
    self.send(msg)
  File "C:\Python26\lib\httplib.py", line 723, in send
    self.connect()
  File "C:\Python26\lib\httplib.py", line 704, in connect
    self.timeout)
  File "C:\Python26\lib\socket.py", line 514, in create_connection
    raise error, msg
IOError: [Errno socket error] [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

我已经在几个不同的页面上尝试过,但我永远无法让 urlopen 方法正确执行.

I've tried it with several different pages but I can never get the urlopen method to execute correctly.

推荐答案

你的代码不是这里的问题.

Your code is not the problem here.

您的 IE 中是否有任何代理设置?

Do you have any Proxy settings in your IE?

这是 urllib.urlopen 的 python 文档:

This says the python documentation for urllib.urlopen:

在Windows环境下,如果没有设置代理环境变量,
代理设置从注册表的 Internet 设置中获取
部分.

In a Windows environment, if no proxy environment variables are set,
proxy settings are obtained from the registry's Internet Settings
section.

这篇关于为什么我不能让 Python 的 urlopen() 方法在 Windows 上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆