Skip to content

Commit 0f8f08a

Browse files
authored
Update doc/examples.rst
Added example of use
1 parent 2abdb1d commit 0f8f08a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

doc/examples.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,21 @@ commit it::
410410
The drawback is that `Table` is a singleton, and if you disable autocommit, it
411411
will be disabled for all instances of that `Table`.
412412

413+
Easy rules with dictionaries
414+
----------------------------
415+
To simplify operations with ``python-iptables`` rules we have included support to define and convert Rules object into python dictionaries.
416+
417+
>>> import iptc
418+
>>> table = iptc.Table(iptc.Table.FILTER)
419+
>>> chain = iptc.Chain(table, "INPUT")
420+
>>> # Create an iptc.Rule object from dictionary
421+
>>> rule_d = {'comment': {'comment': 'Match tcp.22'}, 'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}}
422+
>>> rule = iptc.Rule.from_dict(rule_d)
423+
>>> # Obtain a dictionary representation from the iptc.Rule
424+
>>> rule.to_dict()
425+
{'tcp': {'dport': '22'}, 'protocol': 'tcp', 'comment': {'comment': 'Match tcp.22'}, 'target': 'ACCEPT'}
426+
427+
413428
Known Issues
414429
============
415430

0 commit comments

Comments
 (0)