FTP Client in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back to this tutorial in today's video we're going to learn how to build a simple ftp client in python so let us get right into it all right so ftp stands for file transfer protocol which means of course that we can transfer files via this protocol we can upload files we can download files from to an ftp server and in order to do this in python we need to import the ftp library so ftp lib which is part of the core python stack so we don't need to install anything here and uh what we need to do here is we need to connect to a server so we need to have an ftp server already it can be running locally it can be running in the web somewhere and we're going to connect to that server and then we're going to upload files or download files from our ftp server and this can be very useful if we choose ftp for our backups for example so let's say you have a certain amount of or a certain collection of directories that you would like to back up on your computer on a regular basis maybe weekly maybe monthly maybe daily you want to have some backups and you want to build your own python script to do those backups and maybe i'm going to make a video on that in the future but essentially what you would be doing then is you would create a script here and you would just you know say okay exclude this file include this file look for changes and so on and then you would need something that uploads the files or downloads the files uh depending on what you're trying to do and of course you can use a cloud for that or you can use an ftp server for that and this is what we're going to do in today's video we're going to talk about how we can connect to an ftp server as i said we need to already have that ftp server in my case i have one already and i'm going to also show you all the credentials here because it's not really an important server i just made it for free and you can do so as well so we're going to open up a an ftp client here to see how it works if we already have one if we already have a working ftp client so i'm going to connect to video ftp tut so tutorial dot b placed dot net and i'm going to show you in a second how you can also build your own or get your own ftp server for free they offer a very good package there and uh the user is video ftp tut and the password i'm going to change this after the video is neural one two three so we quick connect and we have to accept this and there you go so we have a bunch of different files here most of them i have already created here i'm going to delete those and here i can now go ahead and upload files create files download files and so on so this is the server and we're going to build a python script that can do that as well of course we're not going to build this full program here but we're going to build the connection and we're going to be able to download and upload from to that server all right so the first thing we're going to do is we're going to create a couple of variables and the first one is going to be the host the host is video ftp touch.beplaced.net in my case in your case it's something else so input that and then we are going to have let me just copy this here we're going to have a user as well and the user is going to be uh in my case video ftp tut and we're going to also have a password and the password is going to be neural 123 in this case now of course you should never store your password in clear text always load it from a file encrypted or something like that or let the user input it at runtime in this case i'm just going to do it like that for demonstrational purposes and i'm going to change that password afterwards anyway so no problem there and once you have that you can open up a connection but for this we need to change one thing we need to change this here we need to save from ftp lib import ftp actually and not just import ftp lib so from ftp import ftp and then we say with ftp host so with this ftp connection which means that it's going to be closed automatically as ftp and lowercase we're just going to log into that ftp server so ftp.login and the user that we're going to use for the login is going to be the user and the password is going to be the password and in order to confirm that this worked we can go ahead and print the welcome message so we can say print ftp dot get welcome so when we run this here let's see what happens 220 welcome to be placed ftp so we got a we got a welcome message here and uh this means that we're now connected we can start downloading or uploading files all right since we're now connected to the server we can actually download some files already but the problem is that we don't have any files on the server yet so we're going to log in with filezilla again when we're going to create some files here so we're going to say i know my test.txt and we're going to open up that file and we're going to put some text in there like hello world this is just a neural nine youtube tutorial like that we can save this we can close this we can upload this now we have a file on the ftp server which we can download with python so for this we need to open up a uh stream first so we need to open up a file stream we're going to say with open and for the local file name we can choose whatever we like so we can say uh what is this here we can say something like test.txt as well it doesn't have to be my test dxd it can be whatever we want it to be and then we're going to open this up in writing binary mode sf4 file and once we have that what we're going to do is we're going to call the ftp function which is called retrieve binary which means we're going to get a binary stream a binary file from the server and for this we need a command we need a string that is a command and ftp command and for this we're going to have to just say r e t r and then we're going to add the file name so we request this particular find a file name and in this case it's going to be my text actually my test.txt txt like that and the whole thing is going to be written to or forwarded to f dot right so to the right function we're not going to call it we're going to refer to it like that and we're going to retrieve 1024 bytes like that in the end we're going to say ftp.quit even though i'm not quite sure if we even need that uh when we're using uh with but we're going to do it like that oh sorry come on and we're going to run this to see what happens so as you can see we got the welcome then the script terminated and now we have a test.txt file and you can see that this test.txt file has the the text that we just entered on the server now we can also go ahead and do the same thing for the upload so we're going to delete that here and we're going to uh upload a file to the ftp server and for this of course we need to create one locally first so we're going to save file and we're going to call this myupload.txt and in here we're going to say hello world this file was created locally like that and once we have that what we're going to do is we're just going to open up another file stream so we're going to say with open my upload txt in reading binary mode sf what we're going to do is we're going to read from that file and we're going to upload it to the ftp server and for this we're going to call a different function we're going to call ftp.storebinary and we're going to execute a different command here s-t-o-r plus the file name that we want it to have remotely so we can choose one file name that we choose locally and then we can name it in a different way remotely so we don't have to call this my upload.txt here so let's just call this upload.txt here without the my and we're going to take f as a source for that upload so that's actually all we need to do here we can go ahead save this and run this and it's done as you can see and if we check in filezilla and we refresh here you can see that we have an upload txt file here and if we view this file you can see hello world this file was created locally one thing that's also important is learning how to download files which are not in the main directory so let's say we have not a file like this here but let's say we have another directory which we call mygear and then we have a file in here so not another directory another file so we say uh other file.txt for example and in here we're going to say view edit i am in a different directory like that so this file we cannot just download it by saying other file.txt um and sometimes uh it's also not the best solution to just you know enter the whole path like slash and slash and slash and so on but sometimes you actually want to navigate through the ftp server and for this we can use the command ftp or the function ftp cwd so this changes the directory and you would just have to specify a directory name in this case we're going to what did i call it my dura did i call it my directory like that um and then with open we're going to just do a basic writing again with open what is this thing here with open uh my special file.60 and writing binary mode sf we're just going to say again ftp dot retrieve binary and we're going to say retrieve plus the file name is actually called what did i call our file txt f.write is the function to look at and we're going to write that many bytes so this should be it and we should now be able to also download that file so as you can see we get that file and it works if i remove that line though so if i remove that statement here actually doesn't work so let's delete this file first if i now run the script it's not going to work we're going to get an error 550 can't open other file.txt no such file or directory because there is no file other file.txt in the main directory all right so last but not least i want to quickly show you how i created my free ftp server now this is a german website so don't be confused you're probably not going to understand all that text if you're not german or if you don't speak german and this is also not the only possibility this is just the one i use because i know this site for quite some while already the basic idea is that you go to this website you say freestyle so this is a free version you get one gigabyte of space two databases we don't even need that and we have access with ftp zero euros zero dollars whatever you can sign up for that you can just enter some information here uh and once you are in the back panel uh we're going to do some settings there so let's jump into that all right so this is what the back end looks like in this case of course it's not too different from any other uh hosting websites or something so i for example i also have neural9.com and there i also have ftp and the back end is not too different there so the basic thing that you need to do is you need to look at ftp or for ftp wherever you are whatever your provider is you just look for an ftp option and if you have that ftp option you can create a username here so for example video ftp touch underline i don't know random or something and then you can create that account you get video ftp.random i can choose a password here uh for example i can choose uh videotutorial123 and i can create the account and then what we get is we get an enable account and we can log in with that account that's basically what you need to do and then you have your ftp server up and running so that's it for this video if you enjoyed i hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 13,924
Rating: 4.961165 out of 5
Keywords: ftp, networking, client, python, programming, tutorial, coding, ftp in python, upload, ftp client in python, computer networking, script
Id: aTS7FBz3KLI
Channel Id: undefined
Length: 13min 0sec (780 seconds)
Published: Sun Jan 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.