Python with sqlalchemy db retrival - python

I am quite new to python and to sqlalchemy.I have written the following netowrk program.
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)
In the above code the line
if session.query(SourcetoPort).filter_by(src_address='packet.dst').all():
does not work as I expect it.What I expect is that it should retrive the entry from sqlalchemy database and if it succeeds (as the output is not NONE) it should execute the following code.
When I try to print that line using print
print "session query", session.query(SourcetoPort).filter_by(src_address='packet.dst').all()
The output that I get is
session query []
Am I doing something wrong.It would be great if someone could point this out.
If I change the above line based on the suggestion as
print session.query(SourcetoPort).filter_by(src_address=packet.dst).count()
I get the following error:
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

If you want to filter items by the contents of a variable, you need to use that variable directly:
session.query(SourcetoPort).filter_by(src_address=packet.dst)
You were instead trying to filter by the string 'packet.dst'.
You made the same mistake when creating the SourcetoPort entry; you probably wanted to store result of the packet.src expression, not the string 'packet.src':
SourcetoPort(src_address=packet.src, port_no=packet_in.in_port)
Note that calling .all() retrieves all matching entries from the database, but you are only using it in a if test. It would be more efficient (potentially much more so) to use .count() instead of .all() to let the database tell us how many items match:
if session.query(SourcetoPort).filter_by(src_address=packet.dst).count():

Related

TypeError: <neo4j.work.result.Result object at 0x7f3f4bd01470> is not JSON serializable

For the below code, I am getting an error,please tell me how to resolve this
class GenerateQuery:
#staticmethod
def get_nlg(graph_query):
# graph = Graph("http://localhost:7474",auth=("neo4j", "pass"))
# graph_response = graph.evaluate(graph_query)
# return graph_response
driver = GraphDatabase.driver("neo4j://localhost:7687", auth=("neo4j","pass"))
with driver.session() as session:
graph_response = session.run(graph_query)
return graph_response
#staticmethod
def product_review(summary_comp,prod_comp):
"""
:param summary_comp: product summary
:param prod_comp: product node name
:return: Summary/Review of the corresponding product
"""
query = u'MATCH(s:Store)<-[r:REVIEWED]-(c:Customer) RETURN s.name as ProductName, r.summary as ProductReview'
graph_response = GenerateQuery.get_nlg(query)
return graph_response
when the result of the above is passed to the below code, it gives an error:
class ProductReview(Action):
def name(self):
return "action_review"
def run(self, dispatcher, tracker, domain):
intent = tracker.latest_message['intent']
summary_comp = tracker.get_slot('summary')
prod_comp = tracker.get_slot('node')
graph_response = GenerateQuery.product_review(summary_comp,prod_comp)
dispatcher.utter_message(json.dumps(graph_response))
The error is:
Traceback (most recent call last):
File "/home/sangeetha/Desktop/RiQue/venv/lib/python3.6/site-packages/sanic/app.py", line 939, in handle_request
response = await response
File "/home/sangeetha/Desktop/RiQue/venv/lib/python3.6/site-packages/rasa_sdk/endpoint.py", line 112, in webhook
return response.json(result, status=200)
File "/home/sangeetha/Desktop/RiQue/venv/lib/python3.6/site-packages/sanic/response.py", line 210, in json
dumps(body, **kwargs),
TypeError: <neo4j.work.result.Result object at 0x7f3f4bd01470> is not JSON serializable
Result is not meant to be serialized, it holds transaction-bound data that are released upon transaction termination.
You must first extract the data before serializing it.
You can change get_nlg with something like:
return [record.data() for record in graph_response]
As a side note, session.run should preferably be replaced with session.read_transaction (a.k.a. a transaction function).

Cassandra 3.7 with Python, 'NoneType' object has no attribute 'encode_message'

I have a simple python client to fetch data from cassandra. The query string itself runs fine using the DBeaver EE client.
def getDataSet(self, station_id, tablename):
prepared_stmt = self.session.prepare ( "SELECT event_time, reading FROM " +
tablename + " WHERE station_id = ?;")
bound_stmt = prepared_stmt.bind([station_id])
rslt = self.session.execute(bound_stmt)
df = pd.DataFrame()
for r in rslt:
df = df.append(r)
return df
def __init__(self):
cluster = Cluster(
contact_points=['127.0.0.1'],
)
self.session = cluster.connect('data')
self.session.row_factory = tuple_factory
self.session.client_protocol_handler = NumpyProtocolHandler
I get the following exception
File "/home/david/git/python-example/src/start/fetchcassandra.py", line 13, in getDataSet
rslt = self.session.execute(bound_stmt)
File "/usr/local/lib/python3.4/dist-packages/cassandra/cluster.py", line 1961, in execute
return self.execute_async(query, parameters, trace, custom_payload, timeout, execution_profile).result()
File "/usr/local/lib/python3.4/dist-packages/cassandra/cluster.py", line 3649, in result
raise self._final_exception
cassandra.cluster.NoHostAvailable: ('Unable to complete the operation against any hosts', {<Host: 127.0.0.1 datacenter1>: AttributeError("'NoneType' object has no attribute 'encode_message'",)})
Most likely the NumpyProtocolHander is not built, so you're assigning None as the protocol handler.
See here for installation notes, and consult your pip log for clues on why it is not being built.

Create an instance from volume in openstach with python-novaclient

I am trying to create an instance from a bootable volume in openstack using python-novaclient.
The steps I am taking are following:
Step1: create a volume with an Image "Centos" with 100GB.
Step2: create an instance with the volume that I created in step1.
However, I must be doing something wrong or missing some information that it is not able to complete the task.
Here are my commands in python shell.
import time, getpass
from cinderclient import client
from novaclient.client import Client
project_name = 'project'
region_name = 'region'
keystone_link = 'https://keystone.net:5000/v2.0'
network_zone = "Public"
key_name = 'key_pair'
user = 'user'
pswd = getpass.getpass('Password: ')
# create a connection
cinder = client.Client('1', user, pswd, project_name, keystone_link, region_name = region_name)
# get the volume id that we will attach
print(cinder.volumes.list())
[<Volume: 1d36203e-b90d-458f-99db-8690148b9600>, <Volume: d734f5fc-87f2-41dd-887e-c586bf76d116>]
vol1 = cinder.volumes.list()[1]
vol1.id
block_device_mapping = {'device_name': vol1.id, 'mapping': '/dev/vda'}
### +++++++++++++++++++++++++++++++++++++++++++++++++++++ ###
# now create a connection with nova and create then instance object
nova = Client(2, user, pswd, project_name, keystone_link, region_name = region_name)
# find the image
image = nova.images.find(name="NETO CentOS 6.4 x86_64 v2.2")
# get the flavor
flavor = nova.flavors.find(name="m1.large")
#get the network and attach
network = nova.networks.find(label=network_zone)
nics = [{'net-id': network.id}]
# get the keyname and attach
key_pair = nova.keypairs.get(key_name)
s1 = 'nova-vol-test'
server = nova.servers.create(name = s1, image = image.id, block_device_mapping = block_device_mapping, flavor = flavor.id, nics = nics, key_name = key_pair.name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/novaclient/v1_1/servers.py", line 902, in create
**boot_kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/v1_1/servers.py", line 554, in _boot
return_raw=return_raw, **kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/base.py", line 100, in _create
_resp, body = self.api.client.post(url, body=body)
File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 490, in post
return self._cs_request(url, 'POST', **kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 465, in _cs_request
resp, body = self._time_request(url, method, **kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 439, in _time_request
resp, body = self.request(url, method, **kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 433, in request
raise exceptions.from_response(resp, body, url, method)
novaclient.exceptions.BadRequest: Block Device Mapping is Invalid: failed to get volume /dev/vda. (HTTP 400) (Request-ID: req-2b9db4e1-f24f-48c6-8660-822741ca52ad)
>>>
I tried to find any documentation so that I can solve this on my own, however, I was not able to.
If anyone has tried this before, I would appreciate there help on this.
Thanks,
Murtaza
I was able to get it to work by using this dictionary:
block_dev_mapping = {'vda':'uuid of the volume you want to use'}
I then called it in the create method like this:
instance = nova.servers.create(name="python-test3", image='', block_device_mapping=block_dev_mapping,
flavor=flavor, key_name="my-keypair", nics=nics)

query from sqlalchemy returns AttributeError: 'NoneType' object

from pox.core import core
import pox.openflow.libopenflow_01 as of
import re
import datetime
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('sqlite:///nwtopology.db', 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,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)
class Tutorial (object):
def __init__ (self, connection):
self.connection = connection
connection.addListeners(self)
# Use this table to keep track of which ethernet address is on
# which switch port (keys are MACs, values are ports).
self.mac_to_port = {}
self.matrix={}
#This will keep track of the traffic matrix.
#matrix[i][j]=number of times a packet from i went to j
def send_packet (self, buffer_id, raw_data, out_port, in_port):
#print "calling send_packet"
#Sends a packet out of the specified switch port.
msg = of.ofp_packet_out()
msg.in_port = in_port
msg.data = raw_data
# Add an action to send to the specified port
action = of.ofp_action_output(port = out_port)
msg.actions.append(action)
# Send message to switch
self.connection.send(msg)
def act_like_hub (self, packet, packet_in):
#flood packet on all ports
self.send_packet(packet_in.buffer_id, packet_in.data,
of.OFPP_FLOOD, packet_in.in_port)
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
# create a Session
#Session = sessionmaker(bind=engine)
#session = Session()
self.mac_to_port[packet.src]=packet_in.in_port
#if self.mac_to_port.get(packet.dst)!=None:
#print "count for dst",session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count(),str(packet.dst)
#if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():
if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar() is not None:
#send this packet
print "got info from the database"
q_res = session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).one()
self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, 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)
#send the rule to the switch so that it does not query the controller again.
msg.actions.append(of.ofp_action_output(port=q_res.port_no))
#push the rule
self.connection.send(msg)
else:
#flood this packet out as we don't know about this node.
print "flooding the first packet"
self.send_packet(packet_in.buffer_id, packet_in.data,
of.OFPP_FLOOD, packet_in.in_port)
#self.matrix[(packet.src,packet.dst)]+=1
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()
def _handle_PacketIn (self, event):
"""
Handles packet in messages from the switch.
"""
packet = event.parsed # This is the parsed packet data.
if not packet.parsed:
log.warning("Ignoring incomplete packet")
return
packet_in = event.ofp # The actual ofp_packet_in message.
#self.act_like_hub(packet, packet_in)
self.act_like_switch(packet, packet_in)
def launch ():
"""
Starts the component
"""
def start_switch (event):
log.debug("Controlling %s" % (event.connection,))
Tutorial(event.connection)
core.openflow.addListenerByName("ConnectionUp", start_switch)
When I run the above code I get the following error:
The problem that I am facing is for some reason if I use
if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar() is not None:
in place of count query.
#if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():
The querying from the database
q_res = session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).first()
self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, packet_in.in_port)
is giving the following error:
DEBUG:core:POX 0.1.0 (betta) going up...
DEBUG:core:Running on CPython (2.7.3/Aug 1 2012 05:14:39)
DEBUG:core:Platform is Linux-3.5.0-23-generic-x86_64-with-Ubuntu-12.04-precise
INFO:core:POX 0.1.0 (betta) is up.
DEBUG:openflow.of_01:Listening on 0.0.0.0:6633
INFO:openflow.of_01:[00-00-00-00-00-02 1] connected
DEBUG:tutorial:Controlling [00-00-00-00-00-02 1]
got info from the database
ERROR:core:Exception while handling Connection!PacketIn...
Traceback (most recent call last):
File "/home/karthik/pox/pox/lib/revent/revent.py", line 234, in raiseEventNoErrors
return self.raiseEvent(event, *args, **kw)
File "/home/karthik/pox/pox/lib/revent/revent.py", line 281, in raiseEvent
rv = event._invoke(handler, *args, **kw)
File "/home/karthik/pox/pox/lib/revent/revent.py", line 159, in _invoke
return handler(self, *args, **kw)
File "/home/karthik/pox/tutorial.py", line 118, in _handle_PacketIn
self.act_like_switch(packet, packet_in)
File "/home/karthik/pox/tutorial.py", line 86, in act_like_switch
self.send_packet(packet_in.buffer_id, packet_in.data,q_res.port_no, packet_in.in_port)
AttributeError: 'NoneType' object has no attribute 'port_no'
got info from the database
ERROR:core:Exception while handling Connection!PacketIn...
This line:
if session.query(exists().where(SourcetoPort.src_address == str(packet.dst))).scalar() is not None:
Is always true. The reason is that scalar() returns None only if there are no rows. However your query looks like SELECT EXISTS (SELECT * FROM source_to_port WHERE source_to_port.src_address=?). This will always return exactly one row with one column. The result will thus be True or False, never None.
Moving on to the line before the line that throws your exception: first() returns None if there are no matches, so q_res is None. Since q_res is None, q_res.port_no on the next line raises an exception.
(Note you can use one() if you want an exception to be thrown if there is no match.)
If you are expecting a match, double-check your data and your filter_by() condition to make sure they are doing what you think they should.
However I recommend that you use one query instead of two using first() or one(). With first(), you branch based on q_res being None or not:
q_res = session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).first()
if q_res is not None:
print "got info from the database"
self.send_packet(....)
...
else:
print "flooding the first packet"
...
Or with one(), you put your "flooding" branch in an exception handler:
from sqlalchemy.orm.exc import (NoResultFound, MultipleResultsFound)
try:
q_res = session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).one()
except NoResultFound:
print "flooding the first packet"
...
# except MultipleResultsFound:
# print "More than one result found! WUT?!"
else:
print "got info from the database"
...
A difference between these two approaches is that one() will ensure there is one and only one result, whereas first() doesn't care if there are multiple results.

How to store RSA encrypted data to postgresql by using pycrypto?

I want to use Public/Private key to secure my UserInfo data. I'm new with PyCrypto and PostgreSQL.
I have some items to clarify:
Are Public Key and Private Key constant values?
If it is constant, how can I store it properly?
Lastly but the most important, how can I store my encrypted data to PostgreSQL? and retrieve it for verification?
Would you guide me on how to dealt with Crypto.PublicKey.RSA as method to secure my data.
Environment: Python 2.5, PyCrypto 2.3, PostgreSQL 8.3 UTF-8 encoding
UserInfo model:
class UserInfo(models.Model):
userid = models.TextField(primary_key = True)
password = models.TextField(null = True)
keyword = models.TextField(null = True)
key = models.TextField(null = True, blank = True)
date = models.DateTimeField(null = True, blank = True)
UPDATES1
tests.py:
# -*- encoding:utf-8 -*-
import os
from os.path import abspath, dirname
import sys
from py23.service.models import UserInfo
from Crypto import Random
# Set up django
project_dir = abspath(dirname(dirname(__file__)))
sys.path.insert(0, project_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'py23.settings'
from django.test.testcases import TestCase
class AuthenticationTestCase(TestCase):
def test_001_registerUserInfo(self):
import Crypto.PublicKey.RSA
import Crypto.Util.randpool
#pool = Crypto.Util.randpool.RandomPool()
rng = Random.new().read
# craete RSA object by random key
# 1024bit
#rsa = Crypto.PublicKey.RSA.generate(1024, pool.get_bytes)
rsa = Crypto.PublicKey.RSA.generate(1024, rng)
# retrieve public key
pub_rsa = rsa.publickey()
# create RSA object by tuple
# rsa.n is public key?, rsa.d is private key?
priv_rsa = Crypto.PublicKey.RSA.construct((rsa.n, rsa.e, rsa.d))
# encryption
enc = pub_rsa.encrypt("hello", "")
# decryption
dec = priv_rsa.decrypt(enc)
print "private: n=%d, e=%d, d=%d, p=%d, q=%d, u=%d" % (rsa.n, rsa.e, rsa.d, rsa.p, rsa.q, rsa.u)
print "public: n=%d, e=%d" % (pub_rsa.n, pub_rsa.e)
print "encrypt:", enc
print "decrypt:", dec
# text to be signed
text = "hello"
signature = priv_rsa.sign(text, "")
# check if the text has not changed
print pub_rsa.verify(text, signature)
print pub_rsa.verify(text+"a", signature)
# userid = models.TextField(primary_key = True)
# password = models.TextField(null = True)
# keyword = models.TextField(null = True)
# key = models.TextField(null = True, blank = True) is it correct to store the public key here?
# date = models.DateTimeField(null = True, blank = True)
userInfo = UserInfo(userid='test1', password=enc[0], key=pub_rsa.n)
userInfo.save()
print "ok"
result here (failed):
======================================================================
ERROR: test_001_registerUserInfo (py23.service.auth.tests.AuthenticationTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\PIDevelopment\workspace37_pydev\pyh23\py23\service\auth\tests.py", line 64, in test_001_registerUserInfo
userInfo.save()
File "C:\Python25\lib\site-packages\django\db\models\base.py", line 458, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "C:\Python25\lib\site-packages\django\db\models\base.py", line 551, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "C:\Python25\Lib\site-packages\django\db\models\manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)
File "C:\Python25\lib\site-packages\django\db\models\query.py", line 1524, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python25\lib\site-packages\django\db\models\sql\compiler.py", line 788, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "C:\Python25\lib\site-packages\django\db\models\sql\compiler.py", line 732, in execute_sql
cursor.execute(sql, params)
File "C:\Python25\lib\site-packages\django\db\backends\util.py", line 15, in execute
return self.cursor.execute(sql, params)
File "C:\Python25\lib\site-packages\django\db\backends\postgresql_psycopg2\base.py", line 44, in execute
return self.cursor.execute(query, args)
DatabaseError: invalid byte sequence for encoding "UTF8": 0x97
HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
----------------------------------------------------------------------
Ran 1 test in 90.047s
FAILED (errors=1)
Your problem is that you are trying to store binary data in a text file. Try armoring the data or use bytea (with proper encoding/decoding).

Categories