[Tutor] Best way to do FTP login?

cs at zip.com.au cs at zip.com.au
Fri Jun 17 18:36:13 EDT 2016


On 17Jun2016 19:07, Michael Selik <michael.selik at gmail.com> wrote:
>On Fri, Jun 17, 2016 at 12:46 PM boB Stepp <robertvstepp at gmail.com> wrote:
>> ftp = FTP('ip_address', 'username', 'password')
>>
>> Or
>>
>> ftp = FTP('ip_address')
>> ftp.login('username', 'password')
>>
>> Most of the examples I am seeing online use the second approach.  Is
>> there some reason why this is to be preferred?
>
>Not that I can see. Perhaps only to show that the login method exists.
>I suggest using it in a context manager.
>
>    py> with FTP('ftp.example.com', 'username', 'password') as ftp:
>    ...     ftp.dir()

I would also recommend looking at the netrc module. This parses the well known 
file .netrc for login and password, avoiding putting it in your code. You can 
put clauses for multiple machine in the .netrc, which lets your code work 
against multiple machines without changes.

Note that the .netrc file still keeps the login and password in the clear, but 
if you lock the permissions down on your .netrc so that only you can read it 
there is a degree of security.

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Tutor mailing list