@@ -135,6 +135,31 @@ Full documentation with API reference is available
135135Examples
136136========
137137
138+ High level abstractions
139+ -----------------------
140+
141+ `` python-iptables `` implements a low-level interface that tries to closely
142+ match the underlying C libraries. The module `` iptc.easy `` improves the
143+ usability of the library by providing a rich set of high-level functions
144+ designed to simplify the interaction with the library, for example::
145+
146+ >>> import iptc
147+ >>> iptc.easy.dump_table('nat', ipv6=False)
148+ {'INPUT': [], 'OUTPUT': [], 'POSTROUTING': [], 'PREROUTING': []}
149+ >>> iptc.easy.dump_chain('filter', 'OUTPUT', ipv6=False)
150+ [{'comment': {'comment': 'DNS traffic to Google'},
151+ 'dst': '8.8.8.8/32',
152+ 'protocol': 'udp',
153+ 'target': 'ACCEPT',
154+ 'udp': {'dport': '53'}}]
155+ >>> iptc.easy.add_chain('filter', 'TestChain')
156+ True
157+ >>> rule_d = {'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}}
158+ >>> iptc.easy.insert_rule('filter', 'TestChain', rule_d)
159+ >>> iptc.easy.dump_chain('filter', 'TestChain')
160+ [{'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}}]
161+ >>> iptc.easy.delete_chain('filter', 'TestChain', flush=True)
162+
138163Rules
139164-----
140165
@@ -546,6 +571,21 @@ or more rules, than commit it:
546571The drawback is that Table is a singleton, and if you disable
547572autocommit, it will be disabled for all instances of that Table.
548573
574+ Easy rules with dictionaries
575+ ----------------------------
576+ To simplify operations with `` python-iptables `` rules we have included support to define and convert Rules object into python dictionaries.
577+
578+ >>> import iptc
579+ >>> table = iptc.Table(iptc.Table.FILTER)
580+ >>> chain = iptc.Chain(table, "INPUT")
581+ >>> # Create an iptc.Rule object from dictionary
582+ >>> rule_d = {'comment': {'comment': 'Match tcp.22'}, 'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}}
583+ >>> rule = iptc.easy.encode_iptc_rule(rule_d)
584+ >>> # Obtain a dictionary representation from the iptc.Rule
585+ >>> iptc.easy.decode_iptc_rule(rule)
586+ {'tcp': {'dport': '22'}, 'protocol': 'tcp', 'comment': {'comment': 'Match tcp.22'}, 'target': 'ACCEPT'}
587+
588+
549589Known Issues
550590============
551591
0 commit comments