from pox.core import core
import pox.openflow.libopenflow_01 as of
import re
import datetime
import time
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql.expression import exists
log = core.getLogger()
engine = create_engine('mysql://root@192.168.129.139/nwtopology', echo=False)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()
class SourcetoPort(Base):
""""""
__tablename__ = 'source_to_port'
id = Column(Integer, primary_key=True)
port_no = Column(Integer)
src_address = Column(String(16),index=True)
#-----------------------------------------
def __init__(self, src_address,port_no):
""""""
self.src_address = src_address
self.port_no = port_no
#create tables
Base.metadata.create_all(engine)
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 ,"DEST" , packet.dst
self.mac_to_port[packet.src]=packet_in.in_port
r_res = session.query(SourcetoPort).filter_by(src_address=str(packet.src)).first()
if r_res is None:
print "inserting an entry"
start = time.time()
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()
end = time.time()
elapsed = end - start
print "elapsed insert time ",elapsed
else:
print "entry already present"
我有一个从一台主机向另一台主机发送数据包的网络。预期的行为是第一次将src_地址、端口对插入到sql数据库中,然后它只应执行检索操作。我使用sqlite在本地数据库中测试了此功能是否正常工作。然后我切换到远程数据库使用mysql。
现在我发现了
if r_res is None:
永远是真的,意思是
r_res=session.query(SourcetoPort).filter_by(src_address=str(packet.src)).first()
总是失败。
我怀疑这与mysql的一些错误的查询/添加(与sqlite的查询/添加不同)有关。
It did print out a warning as below when inserting an entry.
/usr/lib/python2.7/dist-packages/sqlalchemy/engine/default.py:330: Warning: Data truncated for column 'src_address' at row 1
cursor.execute(statement, parameters)
我搞不清到底是什么问题。
基于这些评论,我打印出了我试图插入的src_地址的大小。
print 'length of src addreess',len(str(packet.src))
it prints 17
我把src_地址的大小增加到了17,32和64
问题是我只在程序中更改了列的长度 更改数据库表中列的长度或删除表(创建的新表将具有程序中指定的正确长度)
以下方法可能会在将来为您节省时间。
我们还有一种不用重新创建表来更新列的方法——使用ALTER table query。
请参阅this SO answer中的更多讨论
相关问题 更多 >
编程相关推荐