2011-02-15 42 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,但它的工作原理。謝謝 ! –