使用Python脚本在FTP代理之后建立FTP连接

2024-09-26 22:12:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图连接到ftp代理后面的ftp服务器。 代理服务器不需要用户名和密码,ftp服务器需要。 我搜索了几个帖子:

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?

这是我代码的最新版本。到目前为止,我已经发现要在ftp代理后面打开ftp服务器,可以使用符号ftp://username:password@server。我使用urllib2库来定义代理服务器。在

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()

此代码的输出结果为:

^{pr2}$

我似乎无法与ftp服务器建立ftp连接。 但是,当我使用同一组连接数据并在Total Commander中使用时,连接工作正常。请参阅随附的图片Total Commander ftp connection window。在


Tags: to服务器httphost代理serverftpopener

热门问题