gpt4 book ai didi

python - 通过 networkx 和使用 python 在图中添加节点和边

转载 作者:太空宇宙 更新时间:2023-11-04 10:22:52 25 4
gpt4 key购买 nike

我需要创建一个 IP 地址图并标记它们之间的边,为此我编写了下面的代码。 parseOutput() 的内容列表看起来像这样:

('172.16.254.128', '216.58.208.206')
('216.58.208.206', '172.16.254.128')
('172.16.254.128', '216.58.208.226')
('216.58.208.226', '172.16.254.128')
('172.16.254.128', '8.8.8.8')
('8.8.8.8', '172.16.254.128')
('172.16.254.128', '216.58.208.227')
('172.16.254.128', '216.58.208.227')
('216.58.208.227', '172.16.254.128')
('172.16.254.128', '216.58.208.227')
('172.16.254.128', '216.58.208.227')
...

当我运行这段代码时,出现以下错误:

Traceback (most recent call last):
File "test.py", line 40, in <module>
g.add_nodes_from(nodeList)
File "/usr/local/lib/python2.7/dist-packages/networkx/classes/graph.py", line 429, in add_nodes_from
nn,ndict = n
ValueError: need more than 0 values to unpack

import logging, sys, struct, binascii, socket
from scapy.all import *
import networkx as nx
import matplotlib.pyplot as plt

pkts=rdpcap("pcapFile.pcap",20)

def parsePcap():
IPList = [[] for i in range (20)]
count = 0
for pkt in pkts:
#print pkt.summary()
if pkt.haslayer(IP):
#proto = pkt.getLayer(IP).proto
x = pkt.getlayer(IP).src
y = pkt.getlayer(IP).dst
IPList[count]= (x,y)
#IPList[count].append(proto)
print IPList[count]
count += 1
return IPList


parseOutput = parsePcap()
nodeList = parseOutput
edgeList = parseOutput

g = nx.Graph()

g.add_nodes_from(nodeList)
print g.number_of_nodes
g.add_edges_from(edgeList)

pos = nx.spring_layout(g,scale=1) #default to scale=1
nx.draw(g,pos)

我什至不知道我做错了什么。该文档并没有说明太多所有在线示例似乎都是用与我的代码相同的语法编写的。

最佳答案

您看到的问题是由

引起的
IPList = [[] for i in range (20)]

len(pkts) 小于 20 时,这会导致 parsePcap() 返回带有空列表或末尾列表的序列列表:

parseOutput = [
('172.16.254.128', '216.58.208.206'),
...
('172.16.254.128', '216.58.208.227'),
[], #<---------------- This is causing the problem
]

parseOutput被传递给g.add_nodes_from时,您收到回溯错误消息:

File "/usr/local/lib/python2.7/dist-packages/networkx/classes/graph.py", line 429, in add_nodes_from
nn,ndict = n
ValueError: need more than 0 values to unpack

回想起来,如果仔细想想错误信息你可以看到它告诉你 n 有 0 个值需要解包。这使得检测节点 n 是否为空列表:

In [136]: nn, ndict = []
ValueError: need more than 0 values to unpack

空列表来自 parseOutput


而不是预先分配一个固定大小的列表:

IPList = [[] for i in range (20)]

在 Python 中执行此操作的首选方法是使用 append 方法:

def parsePcap():
IPList = []
for pkt in pkts:
if pkt.haslayer(IP):
x = pkt.getlayer(IP).src
y = pkt.getlayer(IP).dst
IPList.append((x, y))
return IPList

这更容易和更易读,因为你不需要为索引而烦恼数字并增加一个计数器变量。此外,它允许您处理pkts 中的任意数量的项目,而无需首先知道的长度pkts.


另一件需要修复的事情是 nodeList 通常不是与 edgeList 相同。

如果你要声明一个nodeListnodeList 应该是 IP 地址的可迭代,而 edgeList应该是一个可迭代的元组,比如 parseOutput:

nodeList = set([item for pair in parseOutput for item in pair])
print(nodeList)
# set(['216.58.208.206', '216.58.208.227', '172.16.254.128',
# '216.58.208.226', '8.8.8.8'])

但是,由于您的所有节点也在 edgeList 中提及,您可以省略声明节点而只使用

edgeList = parseOutput
g.add_edges_from(edgeList)

g.add_edges_from 将隐式添加节点。


import matplotlib.pyplot as plt
import networkx as nx

parseOutput = [
('172.16.254.128', '216.58.208.206'),
('216.58.208.206', '172.16.254.128'),
('172.16.254.128', '216.58.208.226'),
('216.58.208.226', '172.16.254.128'),
('172.16.254.128', '8.8.8.8'),
('8.8.8.8', '172.16.254.128'),
('172.16.254.128', '216.58.208.227'),
('172.16.254.128', '216.58.208.227'),
('216.58.208.227', '172.16.254.128'),
('172.16.254.128', '216.58.208.227'),
('172.16.254.128', '216.58.208.227'),]

g = nx.Graph()
edgeList = parseOutput
g.add_edges_from(edgeList)

pos = nx.spring_layout(g,scale=1) #default to scale=1
nx.draw(g,pos, with_labels=True)
plt.show()

产量

enter image description here


关于python - 通过 networkx 和使用 python 在图中添加节点和边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31230428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com