Upload Files From Client to Server Without Ftp Python
Python - FTP
FTP or File Transfer Protocol is a well-known network protocol used to transfer files between computers in a network. Information technology is created on client server architecture and tin can be used along with user authentication. It can also be used without authentication only that will be less secure. FTP connection which maintains a electric current working directory and other flags, and each transfer requires a secondary connection through which the data is transferred. Near mutual web browsers tin think files hosted on FTP servers.
The Methods in FTP grade
In python we use the module ftplib which has the below required methods to list the files as we will transfer the files.
Method | Clarification |
---|---|
pwd() | Current working directory. |
cwd() | Modify current working directory to path. |
dir([path[,...[,cb]]) | Displays directory listing of path. Optional call-back cb passed to retrlines(). |
storlines(cmd, f) | Uploads text file using given FTP cmd - for case, STOR file name. |
storbinary(cmd,f[, bs=8192]) | Like to storlines() but is used for binary files. |
delete(path) | Deletes remote file located at path. |
mkd(directory) | Creates remote directory. |
exception ftplib.error_temp | Exception raised when an error lawmaking signifying a temporary error (response codes in the range 400–499) is received.. |
exception ftplib.error_perm | Exception raised when an error code signifying a permanent error (response codes in the range 500–599) is received.. |
connect(host[, port[, timeout]]) | Connects to the given host and port. The default port number is 21, as specified past the FTP protocol.. |
quit() | Closes connection and quits. |
Below are the examples of some of the to a higher place methods.
List the Files
The beneath example uses bearding login to the ftp server and lists the content of the current directory. It treates through the name of the files and directories and stores them as a list. Then prints them out.
import ftplib ftp = ftplib.FTP("ftp.nluug.nl") ftp.login("anonymous", "ftplib-example-one") data = [] ftp.dir(data.append) ftp.quit() for line in data: print "-", line
When we run the above plan, we become the following output −
- lrwxrwxrwx 1 0 0 1 Nov 13 2012 ftp -> . - lrwxrwxrwx one 0 0 3 Nov 13 2012 mirror -> pub - drwxr-xr-x 23 0 0 4096 November 27 2017 pub - drwxr-sr-x 88 0 450 4096 May 04 19:thirty site - drwxr-xr-x nine 0 0 4096 January 23 2014 vol
Changing the Directory
The below program uses the cwd method available in the ftplib module to modify the directory and then fetch the required content.
import ftplib ftp = ftplib.FTP("ftp.nluug.nl") ftp.login("anonymous", "ftplib-example-1") data = [] ftp.cwd('/pub/') modify directory to /pub/ ftp.dir(information.suspend) ftp.quit() for line in data: impress "-", line
When we run the higher up program, we go the following output −
- lrwxrwxrwx i 504 450 xiv Nov 02 2007 FreeBSD -> bone/BSD/FreeBSD - lrwxrwxrwx one 504 450 20 November 02 2007 ImageMagick -> graphics/ImageMagick - lrwxrwxrwx 1 504 450 13 November 02 2007 NetBSD -> os/BSD/NetBSD - lrwxrwxrwx ane 504 450 14 November 02 2007 OpenBSD -> os/BSD/OpenBSD - -rw-rw-r-- 1 504 450 932 January 04 2015 README.nluug - -rw-r--r-- i 504 450 2023 May 03 2005 WhereToFindWhat.txt - drwxr-sr-ten 2 0 450 4096 Jan 26 2008 av - drwxrwsr-x ii 0 450 4096 Aug 12 2004 comp
Fetching the Files
After getting the list of files as shown in a higher place, nosotros tin fetch a specific file past using the getfile method. This method moves a re-create of the file from the remote system to the local organisation from where the ftp connection was initiated.
import ftplib import sys def getFile(ftp, filename): try: ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write) except: impress "Mistake" ftp = ftplib.FTP("ftp.nluug.nl") ftp.login("anonymous", "ftplib-example-1") ftp.cwd('/pub/') alter directory to /pub/ getFile(ftp,'README.nluug') ftp.quit()
When we run the above program, we find the file README.nlug to be present in the local system from where the connection was initiated.
Useful Video Courses
Video
Video
Video
Video
Video
Video
Source: https://www.tutorialspoint.com/python_network_programming/python_ftp.htm
0 Response to "Upload Files From Client to Server Without Ftp Python"
Post a Comment