gpt4 book ai didi

python-iptables如何优化代码

转载 作者:太空狗 更新时间:2023-10-29 20:28:26 25 4
gpt4 key购买 nike

我刚开始学习 python,并且已经编写了一些代码来使用 python-iptables 库设置 iptables。我遇到的问题是我不得不一遍又一遍地重写很多相同的代码行。我对功能有所了解,但不了解 OOP。我在想有一种更好的 OOP 方法来编写这段代码,但我无法理解它。任何指针将不胜感激。代码如下。

import iptc

def dropAllInbound():
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), 'INPUT')
rule = iptc.Rule()
rule.in_interface = 'eth+'
rule.target = iptc.Target(rule, 'DROP')
chain.insert_rule(rule)

def allowLoopback():
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), 'INPUT')
rule = iptc.Rule()
rule.in_interface = 'lo'
rule.target = iptc.Target(rule, 'ACCEPT')
chain.insert_rule(rule)

def allowEstablishedInbound():
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), 'INPUT')
rule = iptc.Rule()
match = rule.create_match('state')
match.state = 'RELATED,ESTABLISHED'
rule.target = iptc.Target(rule, 'ACCEPT')
chain.insert_rule(rule)

def allowHTTP():
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), 'INPUT')
rule = iptc.Rule()
rule.in_interface = 'eth+'
rule.protocol = 'tcp'
match = rule.create_match('tcp')
match.dport = '80'
rule.target = iptc.Target(rule, 'ACCEPT')
chain.insert_rule(rule)

def allowHTTPS():
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), 'INPUT')
rule = iptc.Rule()
rule.in_interface = 'eth+'
rule.protocol = 'tcp'
match = rule.create_match('tcp')
match.dport = '443'
rule.target = iptc.Target(rule, 'ACCEPT')
chain.insert_rule(rule)

def allowSSH():
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), 'INPUT')
rule = iptc.Rule()
rule.in_interface = 'eth+'
rule.protocol = 'tcp'
match = rule.create_match('tcp')
match.dport = '22'
rule.target = iptc.Target(rule, 'ACCEPT')
chain.insert_rule(rule)

def allowEstablishedOutbound():
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), 'OUTPUT')
rule = iptc.Rule()
match = rule.create_match('state')
match.state = 'RELATED,ESTABLISHED'
rule.target = iptc.Target(rule, 'ACCEPT')
chain.insert_rule(rule)

def dropAllOutbound():
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), 'OUTPUT')
rule = iptc.Rule()
rule.in_interface = 'eth+'
rule.target = iptc.Target(rule, 'DROP')
chain.insert_rule(rule)

def defaultAction():
dropAllOutbound()
dropAllInbound()
allowLoopback()
allowEstablishedInbound()
allowEstablishedOutbound()

def getInput():
print 'Default action (1) is most secure '
print 'Default - 1'
print 'HTTP - 2'
print 'HTTPS - 3'
print 'SSH - 4'
print 'Exit - 5'
choices = raw_input('Enter choices (comma Separated) ').split(',')
for action in choices:
if action == "1":
defaultAction()
break
if action == "2":
allowHTTP()
break
if action == "3":
allowHTTPS()
break
if action == "4":
allowSSH()
break
else:
break
getInput()

请注意所有规则如何具有相似的代码行。有没有一种方法可以创建规则生成器对象或类似的东西来最大程度地减少重写该代码?

我添加了以下函数,并在每次脚本运行时调用它,以便刷新规则。

def startClean():
chainIn = iptc.Chain(iptc.Table(iptc.Table.FILTER), 'INPUT')
chainIn.flush()
chainOut = iptc.Chain(iptc.Table(iptc.Table.FILTER), 'OUTPUT')
chainOut.flush()

最佳答案

OOP 用于维护某物的状态。 OOP 适用于具有属性和操作这些属性的方法的某些对象。

class Chair(object):

MAX_WEIGHT = 300

def __init__(self):
super().__init__()

self.weight = 5
self.currentWeight = self.weight
self.holding = None
self.broken = False

def hold(self, item):
self.holding = item
self.currentWeight = self.weight + item.weight
self.checkWeight()

def checkWeight(self):
if self.holding.weight > self.MAX_WEIGHT:
self.broken = True
...

您的代码看起来不错;仅仅为 OOP 重写代码可能比它的值(value)更多。如果您真的想使用 OOP,您可能希望执行如下操作。

class Table(object):
def __init__(self):
self.chain = None
self.rule = None
self.match = None

def setInput(self):
self.chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), 'INPUT')

def setOutput(self):
self.chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), 'OUTPUT')

...

table = Table()
table.setInput()
...

关于python-iptables如何优化代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778244/

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