python ftp

作者: admin 分类: python 发布时间: 2011-01-06 16:18 ė 6 没有评论

python ftp 自己写了个玩玩

#!/usr/bin/python
# -*- coding: cp936 -*-
import ftplib
import os
import socket

class ftpclass:
    ‘help 查看\n cd 进入\n ls 查看\n pwd 路径\n put 上传\n get 下载\n’
    def __init__(self,HOST,USER,PASS):
        self.HOST=HOST
        self.USER=USER
        self.PASS=PASS
        try:
            self.fsock=ftplib.FTP(HOST)
        except (socket.error,socket.gaierror),e:
            print ‘ERROR:cannot conncetion “%s”‘%HOST
            sys.exit()
            return

        try:
            self.fsock.login(USER,PASS)
        except ftplib.error_perm:
            print ‘ERROR:cannot login anonymusly’
            self.fsock.quit()
            sys.exit()
        else:
            print “连接成功”

    def ftpcd(self,cwd):
        cwddir=cwd
        if not cwddir:
            cwddir=”/”
        try:
            self.fsock.cwd(cwddir)
            print “进入目录成功”
        except ftplib.error_perm:
            print ‘ERROR:cannot change dir or not dir’
            return

    def ftpls(self,ls):
        lsdir=ls
        if not lsdir:
            ls=”/”
        try:
            self.fsock.dir(lsdir)
        except ftplib.error_perm:
            print ‘ERROR:cannot ls dir’
            return
           
    def ftppwd(self):
        print self.fsock.pwd()
           
    def ftpput(self,put):
        putdir=put.strip()
        putname=os.path.basename(putdir)
        if not putdir:
            print “没有输入目录”
            return
        try:
            filer=open(putdir,’rb’)
        except IOError,e:
            print “putdir file open error:”,e
            return
        try:
            self.fsock.set_debuglevel(2)
            self.fsock.storbinary(‘STOR %s’ %putname ,filer,2048)
            self.fsock.set_debuglevel(0)
        except ftplib.error_perm:
            print “上传不成功,别问我为什么”
            filer.close
            return
       
    def ftpget(self,get):
        getdir=get.strip()
        getname=os.path.basename(putdir)
        if not getname:
            print “没有可下载的文件或目录”
            return
        try:
            getfiler=open(getdir,’wb’).write
            ftp.retrbinary(‘RETR %s’%getname,getfiler,2048)
        except ftplib.error_perm:
            print “现在不成功”
            getfiler.close
          
           
def main():
    print “欢迎使用奥特曼V1.1FTP”
    HOST=raw_input(“请输入连接的FTP:”)
    if not HOST:
        print “错误地址:为空….退出”
        sys.exit()
    else:
        USER=raw_input(“请输入连接的用户:”)
        if not USER:
            print “用户:为空”
        PASS=raw_input(“请输入连接的密码:”)
        if not PASS:
            print “密码:为空”
    connection=ftpclass(HOST,USER,PASS)
    print ftpclass.__doc__
    while True:
        cmd=raw_input(‘输入你要执行的命令:’)
        if cmd.startswith(‘cd’):
            connection.ftpcd(cmd[cmd.find(“/”):])
        elif cmd.startswith(‘ls’):
            connection.ftpls(cmd[cmd.find(“/”):])
        elif cmd.startswith(‘pwd’):
            connection.ftppwd()
        elif cmd.startswith(‘put’):
            connection.ftpput(cmd[cmd.find(” “):])
        elif cmd.startswith(‘get’):
            connection.ftpget(cmd(cmd.find(” “):])
        else:
            pirnt “你敢输入一个正确的命令吗”

       
if __name__==’__main__’:
    main()

本文出自 小Q,转载时请注明出处及相应链接。

本文永久链接: http://www.linuxqq.com/archives/606.html

0
更多
Ɣ回顶部