简体   繁体   English

通过 Python 中的 FTP 代理与 ftplib 连接?

[英]Connecting with ftplib via FTP proxy in Python?

I am trying to download files from FTP. It works fine at home but it doesn't work when I run through company's.network.我正在尝试从 FTP 下载文件。它在家里工作正常,但当我通过 company's.network 时它不起作用。 I know there is something to do with proxy.我知道与代理有关。 I have looked at a few posts regarding the proxy issue in Python. I have tried to set up a connection to the proxy.我查看了 Python 中关于代理问题的一些帖子。我尝试建立与代理的连接。 It works ok for url but it failed when connecting to FTP. Does anyone know a way to do that?它适用于 url,但在连接到 FTP 时失败。有人知道这样做的方法吗? Thanks in advance.提前致谢。

Below is my code:下面是我的代码:

import os
import urllib
import ftplib
from ftplib import FTP
from getpass import getpass
from urllib.request import urlopen, ProxyHandler, HTTPHandler,     HTTPBasicAuthHandler, \
                                build_opener, install_opener

user_proxy = "XXX"
pass_proxy = "YYY"
url_proxy = "ZZZ"
port_proxy = "89"
url_proxy = "ftp://%s:%s@%s:%s" % (user_proxy, pass_proxy, url_proxy,     port_proxy)
authinfo = urllib.request.HTTPBasicAuthHandler()
proxy_support = urllib.request.ProxyHandler({"ftp" : url_proxy})

# build a new opener that adds authentication and caching FTP handlers
opener = urllib.request.build_opener(proxy_support, authinfo,
                                 urllib.request.CacheFTPHandler)

# install it
urllib.request.install_opener(opener)

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

#ftp is not working
ftp = ftplib.FTP('ftp:/ba1.geog.umd.edu', 'user', 'burnt_data')

The error message I got:我收到的错误信息:

730     # and socket type values to enum constants.
731     addrlist = []
--> 732     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
733         af, socktype, proto, canonname, sa = res
734         addrlist.append((_intenum_converter(af, AddressFamily),

gaierror: [Errno 11004] getaddrinfo failed

I can connect via the proxy using FileZilla by selecting custom FTP proxy with specification:我可以使用 FileZilla 通过代理连接,方法是选择具有以下规范的自定义 FTP 代理:

USER %u@%h %s
PASS %p
ACCT %w

FTP Proxy using FileZilla FTP 使用 FileZilla 的代理

You are connecting using an FTP proxy.您正在使用 FTP 代理进行连接。

FTP proxy cannot work with HTTP, so your test against http:// URL to www.google.com is completely irrelevant and does not prove anything. FTP 代理无法与 HTTP 一起使用,因此您针对www.google.com http:// URL 进行的测试完全无关紧要,并且无法证明任何内容。

FTP proxy works as an FTP server. FTP 代理充当 FTP 服务器。 You connect to the proxy, instead of to the actual server.您连接到代理,而不是连接到实际服务器。 And then use some special syntax of a username (or other credentials) to specify your actual target FTP server and its credentials.然后使用用户名(或其他凭据)的一些特殊语法来指定您的实际目标 FTP 服务器及其凭据。 In your case the special syntax of username is user@host user_proxy .在您的情况下,用户名的特殊语法是user@host user_proxy Your proxy expect the proxy password in FTP ACCT command.您的代理需要 FTP ACCT命令中的代理密码。

This should work for your specific case:这应该适用于您的特定情况:

host_proxy = '192.168.149.50'
user_proxy = 'XXX'
pass_proxy = 'YYY'

user = 'user'
user_pass = 'burnt_data'
host = 'ba1.geog.umd.edu'

u = "%s@%s %s" % (user, host, user_proxy)

ftp = ftplib.FTP(host_proxy, u, user_pass, pass_proxy)

No other code should be needed ( urllib or any other).不需要其他代码( urllib或任何其他代码)。

If the proxy uses a custom port (not 21), use this:如果代理使用自定义端口(不是 21),请使用:

ftp = ftplib.FTP()
ftp.connect(host_proxy, port_proxy)
ftp.login(u, user_pass, pass_proxy)

I am getting this error after using the above answer by @Martin Prikryl:使用@Martin Prikryl 的上述答案后出现此错误:

Traceback (most recent call last):
  File "C:\routftp.py", line 21, in <module>
    ftp.connect(PROXY_HOST, PROXY_PORT)
  File "C:\Python39\lib\ftplib.py", line 162, in connect
    self.welcome = self.getresp()
  File "C:\Python39\lib\ftplib.py", line 244, in getresp
    resp = self.getmultiline()
  File "C:\Python39\lib\ftplib.py", line 230, in getmultiline
    line = self.getline()
  File "C:\Python39\lib\ftplib.py", line 218, in getline
    raise EOFError
EOFError

BTW, my FTP host requires Explicit TLS so I am doing ftp = ftplib.FTP_TLS()顺便说一句,我的 FTP 主机需要显式 TLS,所以我正在做ftp = ftplib.FTP_TLS()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM