3

Im trying to connect to an ftp server that is behind ftp proxy. The proxy server does not require username and password, the ftp server does. I have searched through several posts:

Proxies in python

How to use urllib2 to access ftp/http server using proxy with authentification

How to connect to ftp server via proxy using ftplib

How to specify an authenticated proxy for a python http connection?

Here is the last version of my code. So far I've figured out that to open the ftp server behind an ftp proxy, the notation ftp://username:password@server can be used. I'm using the urllib2 library to define the proxy server.

import urllib2

proxy_host = '101.11.44.84:8021'  # only host name, no scheme (http/ftp)
proxy_handler = urllib2.ProxyHandler({'ftp': proxy_host}) 
auth = urllib2.FTPHandler()
try:
    opener_thru_proxy = urllib2.build_opener(proxy_handler, auth)
except:
    logger.exception('build_opener error')    
    raise
print opener_thru_proxy
try:
    conn = opener_thru_proxy.open('ftp://user:password@100.159.66.113')
except:
    logger.exception('opener thru proxy error')
    raise
print conn.read()
conn.close()

The output of this code yields:

2016-05-23 16:15:28,286 - root - ERROR - opener thru proxy error
Traceback (most recent call last):
  File "<string>", line 293, in <module>
  File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 431, in open
  File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 449, in _open
  File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 409, in _call_chain
  File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 1412, in ftp_open
  File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib2.py", line 1434, in connect_ftp
  File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib.py", line 875, in __init__
  File "C:\_Studna\Python\python-2.7.10.amd64\lib\urllib.py", line 884, in init
  File "C:\_Studna\Python\python-2.7.10.amd64\lib\ftplib.py", line 135, in connect
  File "C:\_Studna\Python\python-2.7.10.amd64\lib\socket.py", line 575, in create_connection
URLError: <urlopen error ftp error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>

It seems that I'm unable to establish the ftp connection with the ftp server. However, when I use the same set of connection data and use it in Total Commander, the connection works correctly. See the image enclosed Total Commander ftp connection window.

Community
  • 1
  • 1
Premysl Vorac
  • 473
  • 6
  • 16
  • 2
    I have managed to connect to the ftp server behind ftp proxy using [Winscp](https://winscp.net/). When I use the connection string `open ftp://login:pwd@server:port -passive=on -rawsettings FtpProxyLogonType=2 ProxyHost=proxyaddress ProxyPort=proxyport ProxyUsername=user`. Can I somehow enter the same parameters when setting up an ftp connection in Python? – Premysl Vorac May 27 '16 at 10:59

0 Answers0