是什么导致urllib2.urlopen()通过代理连接? [英] What is causing urllib2.urlopen() to connect via proxy?

查看:294
本文介绍了是什么导致urllib2.urlopen()通过代理连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在公司网络中读取网址。特别是我正在联系的服务器在一个办公室,而客户端PC在另一个办公室:

I'm trying to read a URL within our corporate network. Spesifically the server I'm contacting is in one office and the client PC is in another:

print(urlopen(r"http://london.mycompany/mydir/").read())

每当我运行此函数时我得到:

Whenever I run this function I get:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
File "C:\Python24\lib\urllib2.py", line 130, in urlopen
  return _opener.open(url, data)
File "C:\Python24\lib\urllib2.py", line 364, in open
  response = meth(req, response)
File "C:\Python24\lib\urllib2.py", line 471, in http_response
  response = self.parent.error(
File "C:\Python24\lib\urllib2.py", line 402, in error
 return self._call_chain(*args)
File "C:\Python24\lib\urllib2.py", line 337, in _call_chain
 result = func(*args)
File "C:\Python24\lib\urllib2.py", line 480, in http_error_default
 raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required

奇怪的是这两台计算机之间没有防火墙 - 由于某种原因,url已决定通过我们通常用于连接的代理连接到Web服务器对于公司以外的内容,在这种情况下失败,因为我没有对其进行身份验证。

The odd thing is that there's no firewall between these two computers - for some reason url has decided to connect to the web-server via the proxy which we'd normally use to connect to content outside the company, and in this case that's failing because I've not authenticated it.

我很确定在客户端PC中发生了故障:我做了nslookup并ping服务器以确认两台计算机之间存在连接,但是,当我使用TCPView for Windows观察事务时,我可以看到python.exe进程连接到一个完全不同的服务器(是的,代理!)。

I'm pretty sure that the fault occurs within the client PC: I did a nslookup and a ping to the server to confirm that there's a connection between the two computers, however when I watch the transaction using TCPView for Windows I can see that the python.exe process is connecting to a completely different server (yes, the proxy!).

那么可能导致这种情况的原因是什么?请注意,os.environ [http_proxy]变量未设置 - 此变量通常用于通过代理服务器进行urllib连接。这不是这里的情况。可能会有其他可能具有相同效果的东西吗?

So what could be causing this? Note that the os.environ["http_proxy"] variable is NOT set - this variable is often used to make urllib connect via a proxy server. That's not the case here. Could there be something else which might have the same effect?

仅供参考,在非常锁定的企业环境中,在Windows XP 32bit上运行Python 2.4.4。

FYI, Running Python 2.4.4 on Windows XP 32bit in a very locked-down corporate environment.

推荐答案

它从系统设置中读取。使用 urllib.FancyURLOpener

It reads from the system settings. Use urllib.FancyURLOpener:

opener = urllib.FancyURLopener({})
f = opener.open("http://london.mycompany/mydir/")
f.read()

这篇关于是什么导致urllib2.urlopen()通过代理连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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