简体   繁体   English

在python中通过不同网络进行FTP

[英]FTP over different networks in python

I and my friend have undertaken a small project as our plan for the summer and we were trying to employ FTP using python as a part of the project. 我和我的朋友已经完成了一个小型项目,作为我们夏季的计划,并且我们正在尝试使用python作为项目的一部分使用FTP。 We can successfully transfer the files over the same network but we have no clue as to how we can transfer files when we connected by internet(by a different network). 我们可以在同一网络上成功传输文件,但是对于通过互联网(通过不同网络)连接时如何传输文件,我们一无所知。 I have added the code for your reference. 我已添加代码供您参考。 I new to both FTP and python,it would be great if somebody can help us out. 我是FTP和python的新手,如果有人可以帮助我们,那就太好了。

Server side program: 服务器端程序:

 #server.py

 from pyftpdlib.ftpserver import DummyAuthorizer
 from pyftpdlib.ftpserver import FTPHandler
 from pyftpdlib.ftpserver import FTPServer

 authorizer = DummyAuthorizer()
 authorizer.add_user("user", "12345", "/", perm="elradfmw")
 authorizer.add_anonymous("/")
 handler = FTPHandler
 handler.authorizer = authorizer
 server = FTPServer(("xxx.xxx.x.x", 2121), handler)
 server.serve_forever()

And the client program: 和客户端程序:

 #client.py
 import ftplib

 fileTransfer = ftplib.FTP()
 fileTransfer.connect("xxx.xxx.x.x",2121)
 fileTransfer.login('user','12345')
 fileTransfer.retrlines('LIST')
 fileTransfer.cwd('/home/royal/MyPrograms/Python')
 fileTransfer.retrbinary('RETR  Florida.mp3',open('club.mp3','wb').write)

I am working behind a NAT. 我正在使用NAT。

You're probably running into firewall issues; 您可能会遇到防火墙问题; using passive mode FTP should help. 使用被动模式FTP应该会有所帮助。 There's a good explanation at that link, but the short version is that FTP, by default, uses "active" mode where the client creates a connection to the server to make a request, then the server creates a new connection to the client to respond. 该链接有一个很好的解释,但简短的版本是FTP默认情况下使用“活动”模式,其中客户端创建与服务器的连接以发出请求,然后服务器创建与客户端的连接以响应。 Most firewalls are configured to block "spontaneous" inbound connections, and unless the firewall is specifically configured to look at the content of the outbound connection from the client and see "ah ha, an FTP request, I should expect an incoming connection from that server very soon", it will block the connection. 大多数防火墙都配置为阻止“自发”入站连接,除非将防火墙专门配置为查看来自客户端的出站连接的内容并看到“啊哈,一个FTP请求,否则我应该期望来自该服务器的入站连接”很快”,它将阻止连接。

Passive mode, on the other hand, has the client create two outbound connections, one for the request and a second one (on a different, randomly-chosen port) that the server will use to send the response. 另一方面,被动模式使客户端创建两个出站连接,一个用于请求,另一个用于服务器(用于在另一个随机选择的端口上),服务器将用于发送响应。 Off-the-shelf router+firewall solutions, in their default configuration, will let all outbound connections go through, so this would make the firewall on the client's side let the connections through. 在默认配置下,现成的路由器+防火墙解决方案将允许所有出站连接通过,因此这将使客户端的防火墙允许连接通过。 Configuring the firewall on the server side would be harder, though, as the incoming connection for the data could be on any port -- unless you narrow down the passive data port range. 不过,在服务器端配置防火墙会更加困难,因为数据的传入连接可以在任何端口上-除非您缩小被动数据端口范围。

So what you should do is: 因此,您应该做的是:

  • Use passive mode. 使用被动模式。
  • Configure the server to accept a certain range of ports (like, say, 34500 to 34510, to pick numbers at random -- it doesn't need to be a very large range) for its passive data transfers 将服务器配置为接受一定范围的端口(例如34500至34510,以随机选择数字-不需要很大范围)以进行被动数据传输
  • Configure the firewall on the server side to allow incoming connections on that port range as well as on your "normal" FTP port (2121 in your case) 在服务器端配置防火墙,以允许该端口范围以及“常规” FTP端口(在您的情况下为2121)上的传入连接

If the issues you're having are firewall issues, that should make it work for you. 如果您遇到的问题是防火墙问题,那应该可以为您解决。 If it's still not working; 如果仍然无法正常工作; you may have another problem, so go ahead and ask a new question! 您可能还有其他问题,请继续提出一个新问题! (Or update this one if it's clearly related to the firewall issues). (或者,如果与防火墙问题明显相关,请更新此文件)。

I think the solution for your problem is Port Forwarding 我认为您的问题的解决方案是端口转发

portforwarding.com portforwarding.com

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

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