2

I have the below topology DOT file (test.dot). This dot file maintains a network topology in Switch-port name manner.

digraph G {
   "R1":"swp1" -> "R3":"swp3"; // Port swp1 of switch R1 is connected to port swp3 of R3
   "R1":"swp2" -> "R4":"swp3";
   "R1":"swp3" -> "R5":"swp3";
   "R1":"swp4" -> "R6":"swp3";
 }

I am using pygraphviz library in python to read the graph.

Source code:

#!/usr/bin/env python
from pygraphviz import *

G = AGraph("test.dot")

for edge in G.edges():
    print edge

OUTPUT:

(u'R1', u'R6')
(u'R1', u'R4')
(u'R1', u'R3')
(u'R1', u'R5')

The issue I am facing is that the API doesn't gives the port information.
How can I get the port information also from the API ?

4

2 回答 2

1

这不是一个非常流畅的界面。但是您可以通过以下方式获取数据:

In [2]: G.get_edge('R1','R3').attr['headport']
Out[2]: u'swp3'

In [3]: for e in G.edges():
    print e,e.attr
   ...:     
(u'R1', u'R3') {u'tailport': u'swp1', u'headport': u'swp3'}
(u'R1', u'R4') {u'tailport': u'swp2', u'headport': u'swp3'}
(u'R1', u'R5') {u'tailport': u'swp3', u'headport': u'swp3'}
(u'R1', u'R6') {u'tailport': u'swp4', u'headport': u'swp3'}
于 2014-11-02T17:30:29.493 回答
0

下面的片段完成了这项工作。
for edge in G.edges(): print edge print "edge-ports:", print edge.attr.get("headport", None)

于 2014-11-03T07:30:49.347 回答