sqlmap源码阅读系列检查是否满足依赖

本文介绍了Python中如何在运行时动态导入模块,特别是使用`__import__()`函数。文章通过sqlmap工具的`_checkDependencies()`函数为例,展示了如何检查依赖库是否存在。当尝试导入如'tkinter.ttk'等模块时,如果缺失则会给出警告。作者分享了阅读sqlmap源码的经验,并指出理解此类功能对于代码维护的重要性。
摘要由CSDN通过智能技术生成

sqlmap --dependencies 可以用来检查sqlmap需要使用的一些依赖是否满足。

通过阅读源码我们知道了,核心是__import__()函数。
异常:ImportError

__import__的好处就是,包名可以作为字符串参数传给__import__()

While writing a code, there might be a need for some specific modules. So we import those modules by using a single line code in Python.

But what if the name of the module needed is known to us only during runtime? How can we import that module? One can use the Python’s inbuilt import() function. It helps to import modules in runtime also.


def _checkDependencies():
    """
    Checks for missing dependencies.
    """

    if conf.dependencies:
    	# 关于依赖的检查的函数
        checkDependencies()

在checkDependencies()中有类似如下的代码。

    try:
        __import__("tkinter.ttk")
        debugMsg = "'tkinter.ttk' library is found"
        logger.debug(debugMsg)
    except ImportError:
        warnMsg = "sqlmap requires 'tkinter.ttk' library "
        warnMsg += "if you plan to run a GUI"
        logger.warn(warnMsg)
        missing_libraries.add('tkinter.ttk')

读了两天sqlmap源码,还是在init()函数内,说明这条路很远啊。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值