3

My code belove gives me the error: socket.gaierror: [Errno 11001] getaddrinfo failed when calling the method ftp.connect().

My question is: why can I connect to google.com but when connecting to an ftp server it gives me error? And how I can connect to the ftp server from behind http proxy?

import ftplib
import urllib.request

# ftp settings
ftpusername = 'abc'
ftppassword = 'xyz'
ftp_host = 'host'
ftp_port = 1234

proxy_url = 'http://username:password@host:port'
proxy_support = urllib.request.ProxyHandler({'http': proxy_url})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)

#url works ok
f = urllib.request.urlopen('http://www.google.com/')
print(f.read(500))

# Connecting to ftp fails
with ftplib.FTP() as ftp:
    ftp.connect(host=ftp_host, ftp_port=port)
    ftp.login(user=ftpusername, passwd=ftppassword)
    print(ftp.getwelcome())
    print(ftp.nlst())
    ftp.close()
    ftp.quit()
patski
  • 309
  • 5
  • 12
  • 2
    HTTP is a completely different protocol than FTP. An HTTP proxy can't help you with FTP. – tadman Mar 06 '18 at 18:59
  • 1
    I'm behind an HTTP proxy. How can I then connect to an ftp server? – patski Mar 06 '18 at 19:01
  • You need a different kind of proxy or you need to use HTTP instead of FTP. – tadman Mar 06 '18 at 19:04
  • 2
    @tadman That's not true. FTP protocol can work through HTTP proxy. Any decent FTP client can do that. For example, check [WinSCP proxy settings](https://winscp.net/eng/docs/ui_login_proxy). – Martin Prikryl Mar 06 '18 at 20:01
  • @MartinPrikryl That depends on the proxy and the client supporting it. – tadman Mar 06 '18 at 20:14
  • @tadman Of course. But it’s a quite common setup. Contrary to your misleading comment that imply that it’s not possible at all. – Martin Prikryl Mar 06 '18 at 20:20
  • @MartinPrikryl Staying focused here, if you can produce a working Python solution, I'll upvote it. The question is not if WinSCP can do it, or if it can be done in theory, but if Python can. – tadman Mar 06 '18 at 20:21
  • @tadman Firefox and Chrome can do it. So an HTTP proxy can help. I'm currently facing quite same issue with Java.(see https://stackoverflow.com/q/56776024/363573) – Stephan Jun 27 '19 at 09:12
  • See [Setting up proxy with FTP with Python](https://stackoverflow.com/q/45472577/850848). – Martin Prikryl Mar 24 '22 at 15:09

2 Answers2

1

You may try to send the FTP url directly to the HTTP proxy.

 f = urllib.request.urlopen('ftp://abc:xyz@host/my-file.ext')

Check also if other FTP clients or browsers succeed in iteracting with the FTP server. If they do, use a network sniffer like Wireshark and observe how they talk to the FTP server.

Stephan
  • 40,082
  • 60
  • 228
  • 319
0

You may use a pasive FTP connection. It may be solves the problem