python访问ftp站点,上传文件 #FTP put file#!/usr/bin/python # -*- coding: utf-8 -*- import ftplibimport os import socket import sys HOST = '10.17.15.26' DIRN = '/file_yes' FILE = 'put.txt' host1 = '10.16.66.168'port1 = 15245def 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('FTP002','#######') except ftplib.error_perm: print 'ERROR: cannot login FTP' f.quit() return #print f.getwelcome() print '*** Logged in as "FTP"' print f.getwelcome() 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.dir() except ftplib.error_perm: print 'ERRORL list "%s" info' % DIRN f.quit() return print '*** This is "%s" folder info' % DIRN try: buf_size = 1024 f.storbinary('stor %s' %FILE, open(FILE, 'rb'),buf_size) except ftplib.error_perm: print 'ERROR: cannot read file "%s"' % FILE os.unlink(FILE) else: print '*** Uploaded "%s" to CWD' % FILE f.quit() returnif __name__ == '__main__': main()