gpt4 book ai didi

python - 使用 Python 使用 ftp 设置代理

转载 作者:行者123 更新时间:2023-12-04 17:50:24 26 4
gpt4 key购买 nike

我们正在尝试开发一个 Python 模块,它将访问 FTP 服务器并将文件下载到我的本地机器。当我们尝试运行模块的 FTP 部分时,它超时了。

我们有一个代理服务器(我们称之为“officeproxy.com:8080”)来处理这个问题,当使用 FileZilla 或 Windows Explorer 等 FTP 客户端访问 FTP 站点时,我们成功了。

我们称这个 ftp 站点为“ftp.cal.com”。用户名是“爸爸”。密码是“tango123”。

到目前为止,我们有:

Proxy = officeproxy.com:8080

FTP = ftp.cal.com

User = papa

PW = tango123

以上不是真实的实体,所以如果你想把它们换成真实的,请成为我的客人。

我需要一个模块来首先加载代理服务然后运行 ​​FTP 部分。

我正在运行 Python 2.7。

到目前为止,我已经四处搜索并拥有此代码。 OP 说它只是一个简短的模块,用于测试与 FTP 的连接并读取一个文件。

(注:我特意把 #放在很多地方不知道怎么填的时候显示,或者其他原因):
import urllib2
# I have filled in the proxy info
proxy_host = 'officeproxy.com:8080'

# I don't think this needs any modification, right?
proxy_handler = urllib2.ProxyHandler({'ftp': proxy_host})

# ditto here
proxy_auth_handler = urllib2.ProxyBasicAuthHandler()

# now here is where I am unsure what to put;
# also, I really need FTP user and FTP password, and NOT Proxy...
# so what do I need to change here?
proxy_auth_handler.add_password(None, proxy_host, proxy_user, proxy_passwd)
opener_thru_proxy = urllib2.build_opener(proxy_handler, proxy_auth_handler)

# I filled in this part
conn = opener_thru_proxy.open('ftp://ftp.cal.com/hello.txt')

# I don't believe I need to change this, right?
print conn.read()

最佳答案

添加它是因为它是最好的结果之一,并且很难找到解决方案。

如果您的代理是 HTTP 代理 你需要认证 那么这是如何:

import socks
import socket

socks.set_default_proxy(socks.HTTP,
proxy_host,
proxy_port,
username=proxy_username,
password=proxy_password
)
socket.socket = socks.socksocket
ftp = FTP(ftp_host)
ftp.set_debuglevel(1)
ftp.login(
user=ftp_user,
passwd=ftp_password
)

否则为 FTP代理马丁的回答有效。您也可以在 FileZilla 中查看 FTP 代理的行为,这将有助于根据您的要求编写代码

希望这对某人有帮助!

关于python - 使用 Python 使用 ftp 设置代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45472577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com