Skynet

          ---------- ---------- 我的新 blog : liukaiyi.cublog.cn ---------- ----------

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            112 Posts :: 1 Stories :: 49 Comments :: 0 Trackbacks

          什么都不說,在代碼里
          #!/usr/bin/env python
          #
          -*- encoding: utf8 -*-
          from ftplib import FTP
          import sys,os,getopt

          opts,args
          =getopt.getopt(sys.argv[1:],'hf:d:i:u:p:')

          def usage():
              
          print '''
          Help Information:
            上傳正常結(jié)束后,會(huì)在上次文件邊創(chuàng)建成功狀態(tài)文件夾,名稱為 [上次文件名.state ]
              -h : Show help information
              -f : local upload file   eg -> /home/user/xx/file.tar
              -d : upload to ftp path  eg -> /x/xx/xxx 
              -i : [optional] Default 122.102.xx.xx
              -u : [optional] Default xx
              -p : [optional] Default *** (xx passwd)
              
          '''


          fip
          ='122.xx.xx.xx'
          fur
          ='xx'
          fpw
          ='123'
          for o,a in opts:
              
          if o=='-h':
                  usage()
                  sys.exit()
              
          if o=='-f' : upload_file=a
              
          if o=='-d' : ftp_path=a
              
          if o=='-i' : fip=a
              
          if o=='-u' : fur=a
              
          if o=='-p' : fpw=a


          ftp 
          = FTP(fip)
          ftp.login(fur,fpw)

          if not ( locals().has_key('ftp_path'and locals().has_key('upload_file') ):
              usage()
              sys.exit()


          # 迭代 創(chuàng)建 目錄 
          to_path='/'
          for sp in ftp_path.split('/')[1:]:
              drs 
          = ftp.nlst(to_path)
              
          if to_path=='/':to_path+=sp
              
          else : to_path+='/'+sp
              
          if not to_path in drs :
                  ftp.mkd(to_path)

          #到 最終 目錄下 
          ftp.cwd(to_path)


          # 上傳準(zhǔn)備
          bufsize = 1024
          file_handler 
          = open(upload_file,'rb')
          file_name
          =os.path.split(upload_file)[1]

          # 判定是否 有 上傳完 狀態(tài)文件夾,如果有刪除
          sfile=to_path+'/'+file_name+'.state'
          if sfile in ftp.nlst(to_path):
              
          print '[Resend] delete original dir state '+sfile
              ftp.rmd( sfile )
              
          # 上傳文件
          ftp.storbinary('STOR %s' % (file_name),file_handler,bufsize)

          # 如果 上傳 文件大小不一 ,不標(biāo)注 成功 上傳 狀態(tài)文件夾 
          if not os.path.getsize(upload_file) == ftp.size(to_path+'/'+file_name) :
              
          print '[Error]  upload to ftp size Different ! '
              sys.exit()

          # 上傳 成功 創(chuàng)建標(biāo)示狀態(tài) 文件夾 
          ftp.mkd(sfile)
          file_handler.close()
          ftp.quit()




          整理 www.aygfsteel.com/Good-Game
          posted on 2009-09-09 10:01 劉凱毅 閱讀(1562) 評(píng)論(1)  編輯  收藏 所屬分類: python

          Feedback

          # re: ftp 上傳(py) 2009-09-22 14:07 Taylor
          download  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 泽普县| 九台市| 全州县| 洪江市| 轮台县| 呼和浩特市| 景谷| 连山| 峨边| 湘阴县| 夹江县| 炎陵县| 丹江口市| 南康市| 大同市| 德保县| 隆尧县| 万州区| 兴和县| 宜宾市| 大同市| 开化县| 改则县| 佛学| 乌拉特前旗| 玛多县| 赣州市| 东方市| 米脂县| 富源县| 甘肃省| 荥经县| 沙雅县| 吉木萨尔县| 河津市| 琼中| 甘肃省| 津市市| 五大连池市| 进贤县| 伊宁县|