本人初学python,用ftplib模块写了个ftp程序,有点C的感觉,还不太专业,正在用cmd模块把代码优化

zlchina1989 2010-12-09 03:58:33
请各位大哥大姐批评指正,本人初学,不怕被批!!!


# -*- coding: utf-8 -*-
#!/usr/bin/python
# Filename: ftp_client.py

from ftplib import FTP
import sys
import os

global f_obj

def update(path):
global f_obj
file = os.path.split(path)[1]
ext = os.path.splitext(file)[1]
try:
if ext in ('.txt', '.htm', 'html'):
f_obj.storline('STOR ' + file, open(file))
else:
f_obj.storbinary('STOR ' + file, open(file, 'rb'), 1024)
except:
print 'read file error!'

def download(path, outfile=None):
global f_obj
if outfile is None:
outfile = sys.stdout
dir = os.path.split(path)[0]
file = os.path.split(path)[1]
ext = os.path.splitext(file)[1]
default_dir = f_obj.pwd()
if dir is not None:
f_obj.cwd(dir)
if len(file) == 0:
print 'please input right file or path>>'
return
try:
if ext in ('.txt', '.htm', '.html', '.png'):
outfile = open(file, 'w')
f_obj.retrlines('RETR ' + file, lambda s, w=outfile.write: w(s+"\n"))
else:
f_obj.retrbinary('RETR ' + file, outfile.write)
except:
print 'download error!'
print 'download success!'
def func_cmd(cmd, outfile = None):
global f_obj
if outfile is None:
outfile = sys.stdout

if cmd == '1':
print 'Current dir files as:'
f_obj.dir()
elif cmd == '2':
try:
str = ''
list = []
dir = raw_input('input dir>>')
list = f_obj.nlst(dir)
' '.join(list)
print list
except:
print 'error'
elif cmd == '3':
print 'you want to upload file to ftp!'
filename = raw_input('please input filename or path>>')
update(filename)
print 'upload succ!'
elif cmd == '4':
print 'you want to download files from tfp!'
filename_path = raw_input('please input filename or path>>')
download(filename_path)
print '------download succ!------'
elif cmd == '5':
print 'Current dir:'
print f_obj.pwd()
elif cmd == '6':
try:
path = raw_input('please input the path>>')
f_obj.cwd(path)
except:
print 'error'
elif cmd == '7':
try:
path = raw_input('please input the dir>>')
f_obj.mkd(path)
except:
print 'error'
elif cmd == '8':
try:
dir = raw_input('please input the dir which you want remove>>')
f_obj.rmd(dir)
except:
print 'error'
elif cmd == '9':
try:
file = raw_input('please input the filename>>')
f_obj.size(file)
except:
print 'error'
elif cmd == '10':
f_obj.quit()
f_obj.close()
print '结束连接......'
sys.exit(0)
else:
print 'no command!'

raw_input('enter any key continue.....')


def main():
global f_obj
host = '192.168.1.12'
user = 'tomy'
password = '111111'

try:
print 'Connect Host PC......'
f_obj = FTP(host)
print f_obj.getwelcome()
except:
print "Can't reach the Ftp host!"
sys.exit(1)

try:
print 'login by user.....'
f_obj.login('19930', '111111')
except:
print "Can't login!"
sys.exit(1)

while(1):
intro_str = '''
All commands as:
1:显示当前文件夹文件列表(含有详细列表信息)
2:显示当前文件夹文件
3:上传文件
4:下载文件
5:显示当前目录
6:跳转进入某目录
7:创建一个新的目录
8:删除一个目录
9:返回一个文件的文件大小
10:退出FTP
'''

print intro_str
cmd = raw_input('please input cmd>>')
func_cmd(cmd)

if __name__ == '__main__':
main()


...全文
486 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlchina1989 2010-12-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 long2015 的回复:]

顶你,
再装个图形界面怎样。tk
[/Quote]
图形界面的话可能要等了,毕竟学习是一步一步来的,不过谢谢支持我这个新手
zlchina1989 2010-12-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 iambic 的回复:]

少用if/elif,多用map来处理分支。
[/Quote]
谢了,对python了解还不够,还想不到有的方法,现在先用cmd模块做了先
long2015 2010-12-10
  • 打赏
  • 举报
回复
顶你,
再装个图形界面怎样。tk
iambic 2010-12-09
  • 打赏
  • 举报
回复
少用if/elif,多用map来处理分支。
zlchina1989 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 zlchina1989 的回复:]
请各位大哥大姐批评指正,本人初学,不怕被批!!!

Python code

# -*- coding: utf-8 -*-
#!/usr/bin/python
# Filename: ftp_client.py

from ftplib import FTP
import sys
import os

global f_obj

def update(path):
global f……
[/Quote]


其实就是跟刚刚开始学习C语言的时候,感觉有点像。
I_NBFA 2010-12-09
  • 打赏
  • 举报
回复
c的感觉是啥感觉

37,718

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧