engine = create_engine('sqlite:///nwtopology.db', echo=False)
Base = declarative_base()
class SourcetoPort(Base):
""""""
__tablename__ = 'source_to_port'
id = Column(Integer, primary_key=True)
port_no = Column(Integer)
src_address = Column(String)
#----------------------------------------------------------------------
def __init__(self, src_address,port_no):
""""""
self.src_address = src_address
self.port_no = port_no
Session = sessionmaker(bind=engine)
session = Session()
self.mac_to_port[packet.src]=packet_in.in_port
if(self.matrix.get((packet.src,packet.dst))==None):
self.matrix[(packet.src,packet.dst)]=0
print "found a new flow"
#create an databse entry with address and port
entry = SourcetoPort(src_address=str(packet.src) , port_no=packet_in.in_port)
#add the record to the session object
session.add(entry)
#add the record to the session object
session.commit()
self.matrix[(packet.src,packet.dst)]+=1
print "incrementing flow count"
#if self.mac_to_port.get(packet.dst)!=None:
if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():
#do stuff if the flow information is already in the databaase.
我对python和sql炼金术和东西很陌生。上面的代码是网络控制器的相关部分。只要有新的数据包进来,上面的代码就会被调用。我的问题是
if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():
这是知道 src_address 是否已经在数据库中的正确/最有效的方法吗?有人可以建议一些更好的方法吗?依靠正数似乎不太死板。