Python with sqlalchemy db retrival

时间:2013-04-14 21:30:39

标签: python sql network-programming sqlalchemy

我是python和sqlalchemy的新手。我写了以下netowrk程序。

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
def act_like_switch (self, packet, packet_in):
    """
    Implement switch-like behavior.
    """
    # Learn the port for the source MAC
    print "RECIEVED FROM PORT ",packet_in.in_port , "SOURCE ",packet.src
    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):
            # create a Session
            #print "creating a db session"
            #Session = sessionmaker(bind=engine)
            #session = Session()        
            self.matrix[(packet.src,packet.dst)]=0
            # Create an entry with address and port
            print "creating a new db entry"
            entry = SourcetoPort(src_address="packet.src" , port_no=packet_in.in_port)
            # Add the record to the session object
            session.add(entry)
            session.commit()
    self.matrix[(packet.src,packet.dst)]+=1
    #if self.mac_to_port.get(packet.dst)!=None:
    if session.query(SourcetoPort).filter_by(src_address=packet.dst).all():
      #send this packet
      self.send_packet(packet_in.buffer_id, packet_in.data,self.mac_to_port[packet.dst], packet_in.in_port)
      #create a flow modification message
      msg = of.ofp_flow_mod()
      #set the fields to match from the incoming packet
      msg.match = of.ofp_match.from_packet(packet)

      #print "SENDING  TO PORT " + str(self.mac_to_port[packet.dst]), packet.dst
      # send the rule to the switch so that it does not query the controller again.
      msg.actions.append(of.ofp_action_output(port=self.mac_to_port[packet.dst]))
      # push the rule
      self.connection.send(msg)

    else:
      #print 'flooding the packet '
      # Flood this packet out as we don't know about this node.
      self.send_packet(packet_in.buffer_id, packet_in.data,
                       of.OFPP_FLOOD, packet_in.in_port)

在上面的代码中

if session.query(SourcetoPort).filter_by(src_address='packet.dst').all():

不能按我的预期工作。我期望它应该从sqlalchemy数据库中检索条目,如果成功(因为输出不是NONE),它应该执行以下代码。

当我尝试使用print

打印该行时
 print "session query", session.query(SourcetoPort).filter_by(src_address='packet.dst').all()

我得到的输出是

session query  []

我做错了什么。如果有人能指出这一点,那就太好了。

如果我根据建议更改上述行

print session.query(SourcetoPort).filter_by(src_address=packet.dst).count()

我收到以下错误:

creating a new db entry
RECIEVED FROM PORT  2 SOURCE  96:74:ba:a9:92:b9
creating a new db entry

ERROR:core:Exception while handling Connection!PacketIn...
Traceback (most recent call last):
  File "ws_thesis/pox/pox/lib/revent/revent.py", line 234, in raiseEventNoErrors
    return self.raiseEvent(event, *args, **kw)
  File "ws_thesis/pox/pox/lib/revent/revent.py", line 281, in raiseEvent
    rv = event._invoke(handler, *args, **kw)
  File "ws_thesis/pox/pox/lib/revent/revent.py", line 159, in _invoke
    return handler(self, *args, **kw)
  File "ws_thesis/pox/tutorial.py", line 137, in _handle_PacketIn
    self.act_like_switch(packet, packet_in)
  File "ws_thesis/pox/tutorial.py", line 101, in act_like_switch
    print session.query(SourcetoPort).filter_by(src_address=packet.dst).count()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2400, in count
    return self.from_self(col).scalar()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2045, in scalar
    ret = self.one()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2014, in one
    ret = list(self)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2057, in __iter__
    return self._execute_and_instances(context)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2072, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1405, in execute

1 个答案:

答案 0 :(得分:2)

如果您想按变量的内容过滤项目,则需要直接使用该变量:

session.query(SourcetoPort).filter_by(src_address=packet.dst)

您尝试按字符串'packet.dst'进行过滤。

创建 SourcetoPort条目时犯了同样的错误;您可能希望存储packet.src表达式的结果,而不是字符串'packet.src'

SourcetoPort(src_address=packet.src, port_no=packet_in.in_port)

请注意,调用.all()会从数据库中检索所有匹配的条目,但您只在if测试中使用它。使用.count()而不是.all()来让数据库告诉我们有多少项匹配会更有效(可能很多更多)

if session.query(SourcetoPort).filter_by(src_address=packet.dst).count():