Python上传文件到服务器

#! /usr/bin/env python
# -*- coding: utf-8 -*-
from ftplib import FTP
from os import path
from optparse import OptionParser
from FtpDownload import spilt_cmd
import socket
import sys
'''
上传终端MR文件到服务器
V1.0    zhai   2009-07-27
'''

def main():
    #example ='-u oracle -w oracle -a 10.62.100.232 -p 21 -s ProjData -l c://tt.txt'
    #获取参数
    #example = sys.argv[1]
    #命令行参数用''隔开
    #cmds = spilt_cmd(example)
    cmds =['-u', 'oracle', '-w', 'oracle', '-a', '10.62.100.232', '-p', '21', '-s', 'ProjData', '-l', 'c://tt.txt']
    print cmds

    parser = OptionParser()

    parser.add_option( '-u', '--user', action='store', type='string', dest='user' )
    parser.add_option( '-w', '--password', action='store', type='string', dest='password' )
    parser.add_option( '-a', '--addr', action='store', type='string', dest='server' )
    parser.add_option( '-p', '--port', action='store', type='int', dest='port' )
    parser.add_option( '-s', '--uploaddir', action='store', type='string', dest='uploaddir' )
    parser.add_option( '-l', '--filename', action='store', type='string', dest='filename' )

    (options, args) = parser.parse_args(cmds)
   
    try:
      socket.setdefaulttimeout(60)
      ftp=FTP()
      try:
        ftp.connect(options.server,options.port)
      except:
        #连接FTP失败
        print 'Code:'+'-70000000'
        return
   
      try:
        ftp.login(options.user,options.password)
      except:
        #登录Ftp失败
        print 'Code:'+'-70000001'
        return

      try:
        ftp.cwd(options.uploaddir)
      except:
        #获取目录失败
        print 'Code:'+'-70000002'
        return
      #删除同名文件
      basefilename=path.basename(options.filename)
      try:
         ftp.delete(basefilename)
      except:
         pass
      #上传文件
      file=open(options.filename,'rb')
      ftp.storbinary('STOR '+basefilename,file)
      file.close()
      #关闭连接
      ftp.quit()
      #上传文件成功
      print 'Code:'+'0'
    except Exception, msg:
      #上传文件失败,msg[0]
      print 'Code:'+'-1'


if __name__ == "__main__":
   main()
   

你可能感兴趣的:(oracle,exception,python,String,action,import)