Python 事件处理程序
我正在尝试实现某种事件处理程序。我尝试使用 Popen 使用外部进程来收集示例网络捕获,它会写入一个 XML 文件。我解析 xml 文件以收集我需要的信息。但我不想在数据包数量达到一定限制之前终止该进程。
def getPacketCount(xmlfile, count, pid):
while 1:
try:
parser = minidom.parse(xmlfile)
wlan = parser.getElementsByTagName('wireless-network')[0]
pkt = wlan.getElementsByTagName('packets')[1]
packetCount = pkt.getElementsByTagName('total').childNodes[0].data
if packetCount>count:
#Call event handler to kill process with given pid.
except AttributeError, TypeError:
print "AttributeError: Accessing file again"`
注意:我正在使用 Django 实现此功能来处理数据库操作。
I am trying to implement an event handler of sort. I am try to collect sample network captures using a external process using Popen and it writes an XML file. I parse the xml file to collect what ever information I require. But I do not want to terminate the process until the number of packets has reached a certain limit.
def getPacketCount(xmlfile, count, pid):
while 1:
try:
parser = minidom.parse(xmlfile)
wlan = parser.getElementsByTagName('wireless-network')[0]
pkt = wlan.getElementsByTagName('packets')[1]
packetCount = pkt.getElementsByTagName('total').childNodes[0].data
if packetCount>count:
#Call event handler to kill process with given pid.
except AttributeError, TypeError:
print "AttributeError: Accessing file again"`
Note: I am implementing this with Django to handle database operations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
django 作为一个 Web 框架,不适合事件驱动编程;它更适合常见的 http 请求/响应周期。
让文件轮询程序成为一个单独的脚本可能更有意义(可能使用 INotify 来避免频繁轮询文件),然后它可以通过向资源发出常规 http 请求来通知更大的 Web 应用程序为此,或更新应用程序使用的底层数据库。
django, being a web-framework, is not suited to event driven programming; it's much better suited for the usual request/response cycle of http.
It may make better sense to make the file-polling program a separate script, (possibly using INotify to avoid the need for frequent polling of the file), and it can then notify the larger web application by making a regular http request to a resource for that purpose, or updating an underlying database the app uses.