See All Titles |
![]() ![]() FTPFTP is a popular way to transfer files from machine to machine across a network. It is convenient because there are FTP clients and FTP servers written for all the popular platforms. FTP servers can work with both private users and anonymous users. The difference is that a private FTP server allows only system users to be able to connect via FTP, whereas an anonymous FTP server allows anyone on the network to connect to it and transfer files without having an account. Keep in mind that configuring an anonymous FTP server always exposes the security of your system. The ftplib module implements the client side of the FTP protocol. You can use it for mirroring FTP sites. Usually the urllib module is used as an outer interface to ftplib. For uploads you probably want to use ftplib. The FTP implementation provides one control port and one data port, which means that the actual transmission of data between client and server machines operates over a separate socket on a completely separate port in order to avoid deadlock problems. Check out the Python Documentation for more information: http://www.python.org/doc/lib/module-ftplib.html Transferring DataThe following example shows how to read data from a FTP site: 1: #!/usr/local/bin/python 2: import ftplib 3: ftp = ftplib.FTP('ftp.lessaworld.com') 4: ftp.login() 5: ftp.cwd('downloads/programs') 6: ftp.retrlines('LIST') 7: file = open('filename.txt', 'w') 8: ftp.retrbinary('RETR filename.txt', file.write, 1024) 9: ftp.quit() Line 2: Imports the ftplib module. Line 3: Creates the FTP object and connects to a host server. Line 4: Establishes an anonymous login. Line 5: Uses the cwd() method to change the directory. Line 6: Retrieves the resulting lines of the provided command. In our case, it lists the content of the directory. Line 7: Creates a file on your local server. Line 8: Retrieves the binary information passed to the FTP server, storing it into the mentioned file object. Tip
Note that the interface uses FTP commands—such as LIST, STOR, and RETR—that you need to know. These commands are part of the FTP specification and have nothing to do with Python. The next example uploads a file to the FTP server: 1: import ftplib 2: ftp = ftblib.FTP("ftp.lessaworld.com") 3: ftp.login("username", "password") 4: filename = "index.html" 5: ftp.storlines("STOR " + filename, open(filename)) 6: filename = "app.exe " 7: ftp.storbinary("STOR " + filename, open(filename, "rb"), 1024) Line 3: Provides a username and password to the FTP server in order to establish a connection. Line 5: Uploads a TEXT file to the server. Line 7: Uploads a binary file to the server.
|
Index terms contained in this sectionanonymous FTP serverscontrol ports data transferring FTP sites data ports File Transfer Protocol (FTP) files uploading to FTP servers FTP (File Transfer Protocol) FTP sites transferring data ftplib module modules ftplib urllib networking protocols ports control data private FTP servers protocols File Transfer (FTP) servers anonymous FTP FTP uploading files private FTP sites FTP transferring data transferring data FTP sites uploading files to FTP servers urllib module |
© 2002, O'Reilly & Associates, Inc. |