首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python逻辑问题

Python逻辑问题
EN

Stack Overflow用户
提问于 2015-05-01 08:31:02
回答 1查看 50关注 0票数 0

我正在使用元素树来解析XML文件,并将数据放到sqlite数据库中。我遇到了一个我认为可以用更好的逻辑解决的问题,我很可能会错过这个问题。我得到一个local variable 'netbios_name' referenced before assignment错误,这也是operating_system变量的情况。我理解我为什么要得到它,但我不确定如何解决这个问题。

任何帮助都将不胜感激。

示例XML数据

代码语言:javascript
运行
复制
<ReportHost name="192.168.26.11"><HostProperties>
<tag name="HOST_END">Sat Apr 25 11:36:08 2015</tag>
<tag name="LastUnauthenticatedResults">1223744168</tag>
<tag name="Credentialed_Scan">false</tag>
<tag name="policy-used">Advanced Scan</tag>
<tag name="patch-summary-total-cves">5</tag>
<tag name="cpe-0">cpe:/o:microsoft:windows_2003_server::sp2 -&gt; Microsoft Windows 2003 Server Service Pack 2</tag>
<tag name="system-type">general-purpose</tag>
<tag name="operating-system">Microsoft Windows Server 2003 Service Pack 2</tag>
<tag name="mac-address">00:1f:19:f5:14:34</tag>
<tag name="traceroute-hop-2">192.168.26.11</tag>
<tag name="traceroute-hop-1">10.100.1.249</tag>
<tag name="traceroute-hop-0">10.100.1.254</tag>
<tag name="host-ip">192.168.26.11</tag>
<tag name="netbios-name">PLUTOAPP01</tag>
<tag name="HOST_START">Sat Apr 25 10:20:43 2015</tag>
</HostProperties>

示例问题代码

代码语言:javascript
运行
复制
def get_details(nessus_file):
    db = sqlite3.connect('database.sqlite')
    cursor = db.cursor()
    try:
        tree = ET.parse(nessus_file)
        for reporthost in tree.findall('/Report/ReportHost'):
            host = reporthost.get('name')
            for tag in reporthost.findall('.//HostProperties/tag'):
                if tag.get('name') == 'netbios-name':
                    netbios_name = tag.text
                elif tag.get('name') == 'operating-system':
                    operating_system = tag.text
                else:
                    pass
                #The if statements above^ are causing my issues along with the execute statement below
                cursor.execute('INSERT INTO hosts(host, netbios_name, operating_system) VALUES(?,?,?)', (host, netbios_name, operating_system,))

            for item in reporthost.findall('ReportItem'):
                sev = item.get('severity')
                name = item.get('pluginName')
                description = item.findtext('description')
                pluginid = item.get('pluginID')

                cursor.execute('INSERT INTO vulns(pluginName, severity, description, pluginID) VALUES(?,?,?,?)', (name,sev,description,pluginid,))
                for cve in item.getiterator('cve'):
                    cursor.execute('INSERT INTO cves(cve) VALUES(?)', (cve.text,))
        db.commit()
        db.close()
    except Exception as e:
        print e
        exit()

create_db()
get_details('file.nessus')
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-01 08:33:22

您正在尝试为每个这样的标记插入netbios名称和操作系统。您需要将insert移出您拥有的for循环,并为两个变量设置默认值:

代码语言:javascript
运行
复制
netbios_name = operating_system = None
for tag in reporthost.findall('.//HostProperties/tag'):
    if tag.get('name') == 'netbios-name':
        netbios_name = tag.text
    elif tag.get('name') == 'operating-system':
        operating_system = tag.text
cursor.execute('INSERT INTO hosts(host, netbios_name, operating_system) VALUES(?,?,?)', (host, netbios_name, operating_system,))

如果XML文档中没有任何一个值,这将插入NULL值。

因为您只需要查找这两个标记,所以也可以通过对属性值进行筛选来搜索特定的元素:

代码语言:javascript
运行
复制
netbios_name = reporthost.find('.//HostProperties/tag[@name="netbios-name"]')
netbios_name = netbios_name and netbios.text
operating_system = reporthost.find('.//HostProperties/tag[@name="operating-system"]')
operating_system = operating_system and operating_system.text
cursor.execute('INSERT INTO hosts(host, netbios_name, operating_system) VALUES(?,?,?)', (host, netbios_name, operating_system,))

[@attrib="value"]语法指示ElementTree查找带有该属性和值的标记;.find()方法查找第一个这样的标记,如果缺少,则返回None。然后,下一行将变量集保留为None,或者从查找的标记中提取文本。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29983809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档