2011-02-15 39 views
1

我打开了一个使用ssh -D localhost:5678 [email protected]的SSH隧道,我想在我的python3应用程序中使用它。无法使用urllib和代理访问网站

#!/usr/bin/python3.1 
# -*- coding:Utf-8 -*- 

import urllib.request 

proxyhand = urllib.request.ProxyHandler({"socks" : "http://localhost:5678"}) 
opener = urllib.request.build_opener(proxyhand) 
page = opener.open("http://www.mysite.com") 

哪里mysite.com只能从server.com网络访问(这就是为什么我使用SSH隧道)。

它有效地访问任何其他网站没有限制,但为mysite.com我有一个连接超时错误。隧道工作,因为我可以访问mysite.com使用Firefox配置as explained here

谢谢

回答

0

如果您使用http作为协议,不socks?因此:

proxyhand = urllib.request.ProxyHandler({"http" : "http://localhost:5678"}) 
+0

对于“http”,我有任何网站的“BadStatusLine”例外。 –

+1

SOCKS对于SSH重定向是正确的。我认为问题在于'urllib'没有SOCKS支持。你可能有另一个箍来跳过,以获得这个工作。试试这个:http://stackoverflow.com/questions/2317849/how-can-i-use-a-socks-4-5-proxy-with-urllib2 – jathanism

+0

它是为Python 2.x,但它的工作原理。谢谢 ! –