How to use urllib2 to access ftp / http server using authentication proxy

Update: see comments for my solution.

My python code uses urllib2 to access an FTP server through a proxy with a user and password. I use both urllib2.ProxyHandler and urllib2.ProxyBasicAuthHandler to implement this, following urllib2 examples :

 1 import urllib2 2 proxy_host = 'host.proxy.org:3128' # only host name, no scheme (http/ftp) 3 proxy_handler = urllib2.ProxyHandler({'ftp': proxy_host}) 4 proxy_auth_handler = urllib2.ProxyBasicAuthHandler() 5 proxy_auth_handler.add_password(None, proxy_host, proxy_user, proxy_passwd) 6 opener_thru_proxy = urllib2.build_opener(proxy_handler, proxy_auth_handler) 7 conn = opener_thru_proxy.open('ftp://ftp.ftpserver.org/targetfile.txt') 8 print conn.read() 

The code stops at line 7.

error messages:

  conn = urllib2.urlopen(remote_fn) File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "/usr/lib/python2.6/urllib2.py", line 391, in open response = self._open(req, data) File "/usr/lib/python2.6/urllib2.py", line 409, in _open '_open', req) File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain result = func(*args) File "/usr/lib/python2.6/urllib2.py", line 1344, in ftp_open fw = self.connect_ftp(user, passwd, host, port, dirs, req.timeout) File "/usr/lib/python2.6/urllib2.py", line 1365, in connect_ftp fw = ftpwrapper(user, passwd, host, port, dirs, timeout) File "/usr/lib/python2.6/urllib.py", line 856, in __init__ self.init() File "/usr/lib/python2.6/urllib.py", line 862, in init self.ftp.connect(self.host, self.port, self.timeout) File "/usr/lib/python2.6/ftplib.py", line 134, in connect self.welcome = self.getresp() File "/usr/lib/python2.6/ftplib.py", line 209, in getresp resp = self.getmultiline() File "/usr/lib/python2.6/ftplib.py", line 195, in getmultiline line = self.getline() File "/usr/lib/python2.6/ftplib.py", line 185, in getline if not line: raise EOFError urllib2.URLError: <urlopen error ftp error: > 

I tested the proxy server by specifying the env variable ftp_proxy and accessing the ftp file using

 wget --proxy-user=proxy_user --proxy-password=proxy_passwd ftp://ftp.ftpserver.org/targetfile.txt 

and he successfully downloaded the file from the ftp server.

How can I improve my code to access an ftp site using authentication proxies?

0
python proxy urllib2
Sep 20 '13 at 20:14
source share

No one has answered this question yet.

See similar questions:

2
Using a Python script to establish an FTP connection behind an FTP proxy

or similar:

1710
What is the difference between a proxy server and a reverse proxy server?
1427
How to access environment variables from Python?
880
Getting git to work with a proxy server
four
Basic python urllib2 authentication
3
Python urllib2.urlopen freezes the script endlessly, even if a timeout is set
one
Urllib2 works fine if I run the program myself, but it throws an error when I add it to cronjob
0
Python 2.6.5 - <urlopen error [errno 1] _ssl.c: 480: error: 14077438: ssl routines: ssl23_get_server_hello: tlsv1 alert internal>
0
use proxy in python to get webpage
0
How can I iterate over a list of proxies using Socksipy



All Articles