python iptables:在端口1234上允许传入TCP流量时出现神秘错误

python iptables:在端口1234上允许传入TCP流量时出现神秘错误,python,iptables,Python,Iptables,我想用Python编写一个iptables脚本。我希望使用python iptables包,而不是调用iptables本身。不过,我很难设置一些基本规则。我想使用过滤器链来接受端口1234上传入的TCP流量。所以我写了这个: import iptc chain = iptc.Chain(iptc.TABLE_FILTER,"INPUT") rule = iptc.Rule() target = iptc.Target(rule,"ACCEPT") match = iptc.Match(rule

我想用Python编写一个iptables脚本。我希望使用python iptables包,而不是调用iptables本身。不过,我很难设置一些基本规则。我想使用过滤器链来接受端口1234上传入的TCP流量。所以我写了这个:

import iptc
chain = iptc.Chain(iptc.TABLE_FILTER,"INPUT")
rule = iptc.Rule()
target =  iptc.Target(rule,"ACCEPT")
match = iptc.Match(rule,'tcp')
match.dport='1234'
rule.add_match(match)
rule.target = target
chain.insert_rule(rule)
但是,当我运行此程序时,我会收到以下消息:

Traceback (most recent call last):
  File "testing.py", line 9, in <module>
    chain.insert_rule(rule)
  File "/usr/local/lib/python2.6/dist-packages/iptc/__init__.py", line 1133, in insert_rule
    self.table.insert_entry(self.name, rbuf, position)
  File "/usr/local/lib/python2.6/dist-packages/iptc/__init__.py", line 1166, in new
    obj.refresh()
  File "/usr/local/lib/python2.6/dist-packages/iptc/__init__.py", line 1230, in refresh
    self._free()
  File "/usr/local/lib/python2.6/dist-packages/iptc/__init__.py", line 1224, in _free
    self.commit()
  File "/usr/local/lib/python2.6/dist-packages/iptc/__init__.py", line 1219, in commit
    raise IPTCError("can't commit: %s" % (self.strerror()))
iptc.IPTCError: can't commit: Invalid argument
Exception AttributeError: "'NoneType' object has no attribute 'get_errno'" in <bound method Table.__del__ of <iptc.Table object at 0x7fcad56cc550>> ignored
回溯(最近一次呼叫最后一次):
文件“testing.py”,第9行,在
链.插入规则(规则)
文件“/usr/local/lib/python2.6/dist packages/iptc/_init__.py”,插入规则第1133行
self.table.insert_条目(self.name、rbuf、position)
文件“/usr/local/lib/python2.6/dist packages/iptc/_init__.py”,第1166行,新格式
对象刷新()
文件“/usr/local/lib/python2.6/dist packages/iptc/_init__.py”,第1230行,刷新
self._free()
文件“/usr/local/lib/python2.6/dist-packages/iptc/_-init__uuuuu.py”,第1224行,无格式
self.commit()
提交中的文件“/usr/local/lib/python2.6/dist packages/iptc/_init__.py”,第1219行
raise IPTCError(“无法提交:%s”%(self.strerror()))
iptc.IPTCError:无法提交:参数无效
异常AttributeError:“'NoneType'对象在忽略中没有属性'get\u errno'”

有没有人有过python iptables方面的经验,可以启发我做错了什么

哦,我刚刚注意到了这个。你能给出最新的一张照片吗?我已经修复了大量的bug,并更新了python iptables以使用最新的iptables版本。如果您仍然遇到问题,请在上开一张罚单

有一件事肯定是不对的,那就是您没有在规则中设置协议:

import iptc
chain = iptc.Chain(iptc.TABLE_FILTER,"INPUT")
rule = iptc.Rule()
设置协议,例如,此处:

rule.protocol = 'tcp'
然后你就应该没事了:

target =  iptc.Target(rule,"ACCEPT")
match = iptc.Match(rule,'tcp')
match.dport='1234'
rule.add_match(match)
rule.target = target
chain.insert_rule(rule)

你是以root用户身份运行脚本吗?是的,我设法加入了其他规则,但这一条不起作用:(我一直在使用python iptables,我很喜欢它。我只是想感谢你创建/使用它。谢谢你,我很感激。0.3.0刚刚发布,并有一些修复-可从github或PyPI获得。)