在Python的ftplib中指定主动模式的外部IP地址。

huangapple go评论59阅读模式
英文:

Specify external IP address for active mode in Python ftplib

问题

我正在使用FTP服务器进行传输时采用主动模式。在WinSCP和FileZilla中,有一个用于网络外部IP的选项。如果我在那些框中提供我的IP,我就能够使用主动模式连接到FTP服务器。但是在Python中,我不知道如何分配那个IP。我可以分配ftp.setpasv(false),但它不会检索目录,而是显示您已登录。但当尝试运行ftp.dir()时,它会抛出错误

异常已发生:TimeoutError
[WinError 10060] 由于在一段时间后连接的一方未能正确响应,或者已建立的连接由于连接的主机未能正确响应而失败的连接尝试失败。这是错误信息。我尝试通过ftp.connect函数设置源地址,但它不起作用,我也尝试忽略返回的IP地址,就像我在stackoverflow上找到的一些答案一样,但也没有起作用。

英文:

I'm using active mode for transfers with my FTP server. In WinSCP and FileZilla there is an option for network external IP. If I give my IP in those boxes, I'm able to connect to the FTP server with active mode. But in Python I don’t know how to assign that IP. I can assign ftp.setpasv(false), but it's not retrieving directories but shows you are logged in. But when try to run ftp.dir(), it throws error

> Exception has occurred: TimeoutError
[WinError 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. This is the error. I tried to set source address via ftp.connect function, but its not working, i tried to ignore return ip like some answers i found in stack overflow, but not working .

答案1

得分: 0

重新实现`FTP.sendport`以忽略`host`参数并发送您所需的IP地址像这样

```python
class MyFTP(FTP):
    def sendport(self, host, port):
        return super(MyFTP, self).sendport("192.0.2.0", port)

ftp = MyFTP(ftp_server)

# 代码的其余部分保持不变

顺便说一下,FTP服务器不是“在...模式下”。它们可以同时完美支持两种模式。更好的解决方案是使用被动模式。

这可能会帮助您解决被动模式的问题:
https://stackoverflow.com/q/55814722/850848


<details>
<summary>英文:</summary>

Re-implement the `FTP.sendport` to ignore `host` argument and send your desired IP address instead. Like this:

class MyFTP(FTP):
def sendport(self, host, port):
return super(MyFTP, self).sendport("192.0.2.0", port)

ftp = MyFTP(ftp_server)

the rest of the code is the same


---

Btw, FTP servers are not *&quot;in ... mode&quot;*. They can perfectly support both modes at the same time. Much better solution is to use the passive mode.

This might help you with passive mode problems:  
https://stackoverflow.com/q/55814722/850848

</details>



huangapple
  • 本文由 发表于 2023年6月16日 03:49:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定