Python with Graphviz examples

We can reproduce the same topology graph as before using the Python Graphviz package which we have installed:

$ python3Python 3.5.2 (default, Nov 17 2016, 17:05:23)>>> from graphviz import Digraph>>> my_graph = Digraph(comment="My Network")>>> my_graph.node("core")>>> my_graph.node("distribution")>>> my_graph.node("access1")>>> my_graph.node("access2")>>> my_graph.edge("core", "distribution")>>> my_graph.edge("distribution", "access1")>>> my_graph.edge("distribution", "access2")

The code basically produces what you would normally write in the DOT language but in a more Pythonic way. You can view the source of the graph before the graph generation:

>>> print(my_graph.source)// My Networkdigraph {        core distribution ...

Get Mastering Python Networking - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.