1

I've opened a ssh tunnel with ssh -D localhost:5678 me@server.com and I want to use it in my python3 application.

#!/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")

Where mysite.com can only be accessed from the network on server.com (that's why I use a ssh tunnel).

It works to access any other website with no limitations but for mysite.com I have a connection timed out error. The tunnel works as I can access mysite.com using firefox configured as explained here.

Thank you

Martin Trigaux
  • 5,311
  • 9
  • 45
  • 58

1 Answers1

0

Should you be using http as the protocol, not socks? Thus:

proxyhand = urllib.request.ProxyHandler({"http" : "http://localhost:5678"})
ciaron
  • 1,089
  • 7
  • 15
  • With "http", I have a "BadStatusLine" exception for any website. – Martin Trigaux Feb 15 '11 at 16:27
  • 1
    SOCKS is correct for the SSH redirect. I think the problem is that `urllib` does not have SOCKS support. You might have another hoop to jump through to get this working. Try this: http://stackoverflow.com/questions/2317849/how-can-i-use-a-socks-4-5-proxy-with-urllib2 – jathanism Feb 15 '11 at 16:35