python 下載 ftp 檔案
阿新 • • 發佈:2020-12-21
#!/usr/bin/python # -*- coding: utf-8 -*- import ftplib import os import socket HOST = 'ops.nginx.com' DIRN = '/home/ftpuser' FILE = 'test.txt' def main(): try: f = ftplib.FTP(HOST) except (socket.error, socket.gaierror): print 'ERROR:cannot reach " %s"' % HOST return print'***Connected to host "%s"' % HOST try: f.login('ftpuser','ftpuser') except ftplib.error_perm: print 'ERROR: cannot login anonymously' f.quit() return print '*** Logged in as "anonymously"' try: f.cwd(DIRN) except ftplib.error_perm:print 'ERRORL cannot CD to "%s"' % DIRN f.quit() return print '*** Changed to "%s" folder' % DIRN try: f.retrbinary('RETR %s' % FILE, open(FILE, 'wb').write) except ftplib.error_perm: print 'ERROR: cannot read file "%s"' % FILE os.unlink(FILE)else: print '*** Downloaded "%s" to CWD' % FILE f.quit() return if __name__ == '__main__': main()
/
/
修改變數主機IP 目錄以及檔名
/
/
修改main函式f.login使用者名稱和密碼
python版本基於
[root@db01 ~]
# python
Python
2.7
.
5
(default, Apr
11
2018
,
07
:
36
:
10
)