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. 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.
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.
You're probably running into firewall issues; using passive mode FTP should help. 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. 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.
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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.