# C:\Python26\code\ import ftplib,getopt,sys,os,socket HOST='192.168.0.0' USERNAME='tb' PASSWD='tb' DIRN='/home/tb/lyb/fl/DZ/MF/' FILE='DZ_MF_TP.xml' def main(): optlist,args=getopt.getopt(sys.argv[1:],'h',["help"]) for o,a in optlist: if o in ("-h","--help"): print 'IP@username@passwd@dir@filename' sys.exit() if len(args)<1: print 'IP@username@passwd@dir@filename' sys.exit() nn=args[0].split('@') if len(nn)<5: print 'IP@username@passwd@dir@filename' sys.exit() HOST=nn[0] USERNAME=nn[1] PASSWD=nn[2] DIRN=nn[3] FILE=nn[4] print '--------------put file to--------------------' try: f=ftplib.FTP(HOST) except (socket.error,socket.gaierror),e: print 'ERROR:cannot reach "%s"'% HOST return print '***CONN: "%s"' % HOST try: f.login(user=USERNAME,passwd=PASSWD) except ftplib.error_perm: print 'ERROR:cannot login anonymously' f.quit() return print '***USER: "%s"'%USERNAME try: f.cwd(DIRN) except ftplib.error_perm: print 'ERROR:cannot CD to "%s"' % DIRN f.quit() return print '***FDIR: "%s"' % DIRN try: # f.retrbinary('RETR %s' % FILE,open(FILE,'wb').write) f.storbinary('STOR %s' %FILE, open(FILE, 'rb')) except ftplib.error_perm: print 'ERROR:cannot read file "%s"' % FILE os.unlink(FILE) else: print '***FILE: "%s"' % FILE f.quit() return if __name__=='__main__': main()
使用时,以Python kk.py 192.168.0.0@tb@tb@/home/tb/kk@zzzz.txt形式调用即可。代码自动拆串并发送至目标IP的目标位置。
原创文章,作者:苏葳,如需转载,请注明出处:https://www.swmemo.com/420.html