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.
I am very new to python and sql alchemy and stuff.the above code is releavnt portions of a network controller.The above piece of code gets called whenever a new packet comes in.My question is
if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():
is this the correct/most efficient way to know if the src_address is already in the databse.?Can someone suggest some better method.relying on positive count doesn't seem too rigid.