python-命令行,参数解析,日志管理框架(cmd,argparse,logging模块)_def __init__(self,log_dir_name) self

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上网络安全知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注网络安全)
img

正文

代码

lady_killer.py

cli.py

hello.py

log.py

constants.py

Linux中展示

总结

参考


前段时间的某篇文章中,提及到了参数解析,说学了之后进行分享,最近忙了一阵子,有时间就写了一个简单的框架,适合在Mac或Linux上运行的工具等,有参考sqlmap和一些官方文档。

框架

框架结构

  • lady_killer.py 调用cli
  • cli.py 继承Cmd模块,使用argparse模块进行参数解析,调用hello进行测试
  • hello.py 一些简单的输出,调用log进行日志模块的测试
  • log.py  继承logging模块,进行日志管理
  • constans.py 常量,也可理解为配置文件

代码

lady_killer.py

"""
--coding:utf-8--
@File: lady_killer.py
@Author:frank yu
@DateTime: 2020.08.19 15:38
@Contact: frankyu112058@gmail.com
@Description:main file
"""
from cli import Cli


def start():
    cli = Cli()
    cli.cmdloop()


def main():
    start()


if __name__ == "__main__":
    main()

cli.py

"""
--coding:utf-8--
@File: cli.py
@Author:frank yu
@DateTime: 2020.08.19 15:46
@Contact: frankyu112058@gmail.com
@Description:module to combine and Cmd and argparse
"""
import argparse
import sys
from cmd import Cmd

from hello import Hello


class Cli(Cmd):
    def __init__(self):
        """
        function: __init__
        :return: None
        """
        title = \
            '''
        +++                +++++        ++++++++++++    ++++       ++++    
        +++               +++ +++       +++++++++++++      +++   +++
        +++              +++   +++      +++        +++        +++
        +++             +++++++++++     +++        +++        +++
        +++++++++++    +++       +++    +++++++++++++         +++
        +++++++++++   +++         +++   +++++++++++           +++
        
        Welcome to LADY, a Command and Logging Frame made by lady_killer9(https://blog.csdn.net/lady_killer9)
        version 1.0.0
        use hello -h/--help to show options
        Hit '<ctrl-c>' or 'exit' to shutdown LADY.
            '''
        display = "\033[1;36;40m{}\033[0m".format(title)
        self.do_clear(None)
        print(display)
        Cmd.__init__(self)
        self.prompt = 'lady_killer>'

    def emptyline(self):
        """
        Called when an empty line is entered in response to the prompt.
        If this method is not overridden, it repeats the last nonempty
        command entered.
        """
        return

    def do_hello(self, cmd):
        """
        function:run hello module to test program
        :param cmd: command from console
        :return: None
        """
        commond = cmd.split()
        # print(commond)
        parse = argparse.ArgumentParser(description='Process some integers.')
        parse.prog = "hello"
        parse.usage = parse.prog + " -l [-d] [-i] [-w] [-e] [-c]"
        parse.add_argument('-d', '--debug', type=str, default="this is debug message",
                           help="print message with logging.DEBUG")
        parse.add_argument('-i', '--info', type=str, default="this is info message",
                           help="print message with logging.INFO")
        parse.add_argument('-w', '--warning', type=str, default="this is warning message",
                           help="print message with logging.WARN")
        parse.add_argument('-e', '--error', type=str, default="this is error message",
                           help="print message with logging.ERROR")
        parse.add_argument('-c', '--critical', type=str, default="this is critical message",
                           help="print message with logging.CRITICAL")
        parse.add_argument('-l', type=str, choices=["debug", "info", "warn", "error", "critical"],
                           help="set log level(debug info warn error critical)")
        args = parse.parse_args(commond)
        # print(args)
        kwargs = dict()
        kwargs['d_info'] = args.debug
        kwargs['i_info'] = args.info
        kwargs['w_info'] = args.warning
        kwargs['e_info'] = args.error
        kwargs['c_info'] = args.critical
        kwargs['l'] = args.l
        hello = Hello()
        hello.set_log(**kwargs)
        hello.debug(**kwargs)
        hello.info(**kwargs)
        hello.warning(**kwargs)
        hello.error(**kwargs)
        hello.critical(**kwargs)

    def do_exit(self, msg):
        """
        function: close the cmd
        :param msg: param about service to stop
        :return: None
        """
        print('Bye!!!')
        sys.exit(0)

    def do_clear(self, msg):
        """
        function: clear screen
        """
        print("\033c")

继承Cmd模块,使用argparse模块进行参数解析(空格分隔),输出标题,添加exit,clear,hello三条命令,hello中添加部分参数

hello.py

"""
--coding:utf-8--
@File: hello.py
@Author:frank yu
@DateTime: 2020.08.19 16:02
@Contact: frankyu112058@gmail.com
@Description:this is module for test
"""
from log import *

log = Log()


class Hello:
    def __init__(self):
        pass

    def debug(self, **kwargs):
        info = kwargs.get('d_info')
        log.log_show_store(info, logging.DEBUG)

    def info(self, **kwargs):
        info = kwargs.get('i_info')
        log.log_show_store(info, logging.INFO)

    def warning(self, **kwargs):
        info = kwargs.get('w_info')
        log.log_show_store(info, logging.WARN)

    def error(self, **kwargs):
        info = kwargs.get('e_info')
        log.log_show_store(info, logging.ERROR)

    def critical(self, **kwargs):
        info = kwargs.get('c_info')
        log.log_show_store(info, logging.CRITICAL)

    def set_log(self, **kwargs):
        level = kwargs.get('l')
        # print(level)
        if level == "debug":
            log.logger.setLevel(logging.DEBUG)
        elif level == "info":
            log.logger.setLevel(logging.INFO)
        elif level == "warn":
            log.logger.setLevel(logging.WARN)
        elif level == "error":
            log.logger.setLevel(logging.ERROR)
        elif level == "critical":
            log.logger.setLevel(logging.CRITICAL)
        else:
            log.log_show("please input -l", logging.CRITICAL)
            exit(0)

log.py

"""
--coding:utf-8--
@File: log.py.py
@Author:frank yu
@DateTime: 2020.08.19 15:22
@Contact: frankyu112058@gmail.com
@Description:log module
"""
import logging
import os
import random
import sys
import threading
import time
from shutil import copyfile
from constants import *
from colorlog import ColoredFormatter


## 写在最后

**在结束之际,我想重申的是,学习并非如攀登险峻高峰,而是如滴水穿石般的持久累积。尤其当我们步入工作岗位之后,持之以恒的学习变得愈发不易,如同在茫茫大海中独自划舟,稍有松懈便可能被巨浪吞噬。然而,对于我们程序员而言,学习是生存之本,是我们在激烈市场竞争中立于不败之地的关键。一旦停止学习,我们便如同逆水行舟,不进则退,终将被时代的洪流所淘汰。因此,不断汲取新知识,不仅是对自己的提升,更是对自己的一份珍贵投资。让我们不断磨砺自己,与时代共同进步,书写属于我们的辉煌篇章。**


需要完整版PDF学习资源私我





**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注网络安全)**
![img](https://img-blog.csdnimg.cn/img_convert/cc486a324cbb983305e23c53bc421f9c.png)

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

贵投资。让我们不断磨砺自己,与时代共同进步,书写属于我们的辉煌篇章。**


需要完整版PDF学习资源私我





**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注网络安全)**
[外链图片转存中...(img-zpZU9CSj-1713394516278)]

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值