python cmd模块_Python的cmd模块

0x00 关于cmd模块

使用cmd模块创建的命令行解释器可以循环读取输入的所有行并且解析它们

0x01 cmd模块的一些常用方法:

cmdloop():类似与Tkinter的mainloop,运行Cmd解析器

onecmd(str):读取输入,并进行处理,通常不需要重载该函数,而是使用更加具体的do_command来执行特定的命名

emptyline():当输入空行时调用该方法

default(line):当无法识别输入的command时调用该方法

completedefault(text,line,begidx,endidx):如果不存在针对的complete_*()方法,那么会调用该函数

precmd(line):命令line解析之前被调用该方法

postcmd(stop,line):命令line解析之后被调用该方法

preloop():cmdloop()运行之前调用该方法

postloop():cmdloop()退出之后调用该方法

0x02 用cmd模块简单实现shell命令

#!/usr/bin/env python

#-*- coding:utf-8 -*-

import sys

import os

import socket

from cmd import Cmd

class ClassShell(Cmd):

"""docstring for ClassShell"""

def __init__(self):

Cmd.__init__(self)

os.chdir("C:/Users/WYB_9/Desktop")

hostName = socket.gethostname()

self.prompt = "reber@" + hostName + " " + os.path.abspath('.') + "\n$ "

def help_dir(self):

print "dir [path]"

def do_dir(self, arg):

if not arg:

print "\n".join(os.listdir('.'))

elif os.path.exists(arg):

print "\n".join(os.listdir(arg))

else:

print "no such path exists"

def help_ls(self):

print "ls [path]"

def do_ls(self, arg):

if not arg:

print "\n".join(os.listdir('.'))

elif os.path.exists(arg):

print "\n".join(os.listdir(arg))

else:

print "no such path exists"

def help_pwd(self):

print "pwd"

def do_pwd(self, arg):

print os.path.abspath('.')

def help_cd(self):

print "cd [path]"

def do_cd(self, arg):

hostName = socket.gethostname()

if not arg:

os.chdir("C:/Users/WYB_9/Desktop")

self.prompt = "reber@" + hostName + " " + os.path.abspath('.') + "\n$ "

elif os.path.exists(arg):

os.chdir(arg)

self.prompt = "reber@" + hostName + " " + os.path.abspath('.') + "\n$ "

else:

print "no such path"

def help_clear(self):

print "clear"

def do_clear(self, arg):

i = os.system('cls')

def help_cat(self):

print "cat filename"

def do_cat(self, arg):

if os.path.exists(arg):

with open(arg,"r") as f:

data = f.read()

print data

else:

print "no such file exists"

def help_mv(self):

print "mv oldfilename newfilename"

def do_mv(self, arg):

oldfilename,newfilename = arg.split()

if os.path.exists(oldfilename):

os.rename(oldfilename,newfilename)

else:

print "no such file:" + oldfilename

def help_touch(self):

print "touch filename"

def do_touch(self, arg):

with open(arg, "w") as f:

pass

def help_rm(self):

print "rm filepath"

def do_rm(self, arg):

if os.path.exists(arg):

os.remove(arg)

else:

print "no such file:" + arg

def help_cp(self):

print "cp oldfilepath newfilepath"

def do_cp(self, arg):

oldfilepath,newfilepath = arg.split()

if os.path.exists(oldfilepath):

with open(oldfilepath, "r") as f:

data = f.read()

with open(newfilepath, "w") as f:

f.write(data)

else:

print "no such path:" + oldfilepath

def help_exit(self):

print "input exit will exit the program"

def do_exit(self, arg):

print "Exit:",arg

sys.exit()

if __name__ == '__main__':

shell = ClassShell()

shell.cmdloop()

0x03 程序执行结果如下

若未作声明则文章版权归本人(@reber)所有,转载请注明原文链接:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值