lettura file json lato server,
creazione elementi db manca: thread di gestione delle richieste e della logica
This commit is contained in:
parent
6b44aea4df
commit
f5d9786222
5 changed files with 92 additions and 34 deletions
2
examplejson.txt
Normal file
2
examplejson.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
echo '[{"name": "Nome", "starttime": "ORA", "endtime": "EndTme"}]' | nc 127.0.0.1 9999
|
||||
|
|
@ -1,52 +1,70 @@
|
|||
import SocketServer
|
||||
|
||||
import sys,os
|
||||
|
||||
import logging
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
# include data object
|
||||
from techrec import *
|
||||
|
||||
""" CONNECTION HANDLER """
|
||||
class ConnHandler(SocketServer.BaseRequestHandler):
|
||||
def __init__(self, db, *args, **keys):
|
||||
self.db = db
|
||||
SocketServer.BaseRequestHandler.__init__(self, *args, **keys)
|
||||
|
||||
def handle(self):
|
||||
|
||||
self.data = self.request.recv(1024).strip()
|
||||
logging.info("Connection from " % ( format(self.client_address[0]) ))
|
||||
print self.data
|
||||
self.request.sendall(self.data.upper())
|
||||
logging.debug("DATA: %s - %s" % (self.client_address[0], self.data) )
|
||||
|
||||
# asjson = "[{name: 'Nome', starttime: 'StartTime', endtime: 'EndTime'}]"
|
||||
# asjson = '[{"name": "Nome", "starttime": "StarTTTIME", "endtime": "EndTme"}]'
|
||||
# print "JSon example", asjson
|
||||
print "Data", self.data
|
||||
print "DATA U", unicode( self.data )
|
||||
tmp = Rec(asjson=self.data)
|
||||
print "Creato REC: ", tmp
|
||||
|
||||
if tmp.err():
|
||||
respmsg = "Data ERROR"
|
||||
else:
|
||||
respmsg = "OK"
|
||||
|
||||
self.db.add( tmp )
|
||||
self.db.printall()
|
||||
self.request.sendall( respmsg )
|
||||
|
||||
def handler_factory(db):
|
||||
def createHandler(*args, **keys):
|
||||
return ConnHandler(db, *args, **keys)
|
||||
return createHandler
|
||||
|
||||
""" MAIN SERVER """
|
||||
class recserver:
|
||||
def __init__(self, host="localhost", port=9999):
|
||||
self.host = host
|
||||
self.port = port
|
||||
|
||||
self.db = RecDB()
|
||||
|
||||
def start(self):
|
||||
self.server = SocketServer.TCPServer((self.host, self.port), ConnHandler)
|
||||
try:
|
||||
# self.server = SocketServer.TCPServer((self.host, self.port), ConnHandler)
|
||||
self.server = SocketServer.TCPServer((self.host, self.port), handler_factory( self.db ) )
|
||||
except:
|
||||
sys.exit("Bind error.")
|
||||
|
||||
logging.info("Server ready to serve forever.")
|
||||
self.server.serve_forever()
|
||||
|
||||
|
||||
def loadall():
|
||||
|
||||
"""
|
||||
# Testing DB
|
||||
db = RecDB()
|
||||
|
||||
a = Rec(name="Mimmo1",starttime="ora",endtime="fine")
|
||||
b = Rec(name="Mimmo2",starttime="ora",endtime="fine")
|
||||
c = Rec(name="Mimmo3",starttime="ora",endtime="fine")
|
||||
|
||||
db.add( a )
|
||||
db.add( b )
|
||||
db.add( c )
|
||||
|
||||
db.printall()
|
||||
|
||||
db.search( Rec(name="Mimmo1") )
|
||||
|
||||
|
||||
sys.exit()
|
||||
|
||||
"""
|
||||
# Loading Server TODO: make thread
|
||||
r = recserver()
|
||||
r.start()
|
||||
|
@ -55,4 +73,4 @@ def loadall():
|
|||
# Loading Server
|
||||
if __name__ == "__main__":
|
||||
loadall()
|
||||
|
||||
|
||||
|
|
Binary file not shown.
|
@ -1,9 +1,18 @@
|
|||
import logging
|
||||
import logging
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
import sys
|
||||
|
||||
from sqlalchemy import create_engine, Column, Integer, String
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
try:
|
||||
from sqlalchemy import create_engine, Column, Integer, String
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
except:
|
||||
sys.exit("No SQLAlchemy.")
|
||||
|
||||
|
||||
import json
|
||||
|
||||
import yaml
|
||||
|
||||
"""
|
||||
This class describe a single Record (Rec() class) and the
|
||||
|
@ -15,6 +24,9 @@ Base = declarative_base()
|
|||
""" ************
|
||||
RECORD ABSTRACTION
|
||||
************ """
|
||||
QUEUE = 0
|
||||
RUN = 2
|
||||
DONE = 4
|
||||
|
||||
class Rec(Base):
|
||||
|
||||
|
@ -23,15 +35,41 @@ class Rec(Base):
|
|||
name = Column(String)
|
||||
endtime = Column(String)
|
||||
starttime = Column(String)
|
||||
|
||||
def __init__(self, name="", starttime="", endtime=""):
|
||||
self.name = name
|
||||
self.starttime = starttime
|
||||
self.endtime = endtime
|
||||
self.id = None
|
||||
state = Column(Integer)
|
||||
|
||||
def __init__(self, name="", starttime="", endtime="",asjson=""):
|
||||
self.error = 0
|
||||
|
||||
if len(asjson) == 0:
|
||||
self.name = name
|
||||
self.starttime = starttime
|
||||
self.endtime = endtime
|
||||
else:
|
||||
try:
|
||||
# dec = json.loads( unicode(asjson) )
|
||||
dec = yaml.load( asjson )
|
||||
except:
|
||||
self.error = 0
|
||||
self.name = dec[0]['name']
|
||||
self.starttime = dec[0]['starttime']
|
||||
self.endtime = dec[0]['endtime']
|
||||
# self.id = None
|
||||
self.state = QUEUE
|
||||
print "DECC" , dec
|
||||
print "NAME", self.name
|
||||
print "Sstart", self.starttime
|
||||
print "END", self.endtime
|
||||
|
||||
def err(self): return self.error
|
||||
|
||||
def setrun(self):
|
||||
self.state = RUN
|
||||
|
||||
def setdone(self):
|
||||
self.state = DONE
|
||||
|
||||
def __repr__(self):
|
||||
return "<Rec('%s','%s','%s', '%s')>" % (self.id, self.name, self.starttime, self.endtime)
|
||||
return "<Rec('%s','%s','%s', '%s', '%s')>" % (self.id, self.name, self.starttime, self.endtime, self.state)
|
||||
|
||||
|
||||
""" ************
|
||||
|
@ -69,7 +107,7 @@ class RecDB:
|
|||
|
||||
logging.info("Looking for %s" % recfilter)
|
||||
|
||||
self.session.query( Rec ).filter( Rec.name=name ).first()
|
||||
# self.session.query( Rec ).filter( Rec.name=name ).first()
|
||||
|
||||
for row in records:
|
||||
print "Found:", row
|
||||
|
|
Binary file not shown.
Loading…
Reference in a new issue