First Commit

This commit is contained in:
antifaz 2013-08-28 01:09:53 +02:00
parent 82fe990b6f
commit 6b44aea4df
4 changed files with 133 additions and 0 deletions

58
server/server.py Normal file
View file

@ -0,0 +1,58 @@
import SocketServer
import sys,os
import logging
logging.basicConfig(level=logging.INFO)
from techrec import *
""" CONNECTION HANDLER """
class ConnHandler(SocketServer.BaseRequestHandler):
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())
""" MAIN SERVER """
class recserver:
def __init__(self, host="localhost", port=9999):
self.host = host
self.port = port
def start(self):
self.server = SocketServer.TCPServer((self.host, self.port), ConnHandler)
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()
# Loading Server
if __name__ == "__main__":
loadall()

BIN
server/techrec.db Normal file

Binary file not shown.

75
server/techrec.py Normal file
View file

@ -0,0 +1,75 @@
import logging
logging.basicConfig(level=logging.DEBUG)
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
"""
This class describe a single Record (Rec() class) and the
records manager (RecDB() class)
"""
Base = declarative_base()
""" ************
RECORD ABSTRACTION
************ """
class Rec(Base):
__tablename__ = 'rec'
id = Column(Integer, primary_key=True)
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
def __repr__(self):
return "<Rec('%s','%s','%s', '%s')>" % (self.id, self.name, self.starttime, self.endtime)
""" ************
RecDB
************ """
class RecDB:
def __init__(self):
self.engine = create_engine('sqlite:///techrec.db', echo=True)
self.conn = self.engine.connect()
Base.metadata.create_all(self.engine) # create Database
Session = sessionmaker(bind=self.engine)
self.session = Session()
def add(self, simplerecord):
logging.debug("New Record: %s" % simplerecord)
self.session.add( simplerecord )
self.session.commit()
# print all records
def printall(self):
logging.info("DB")
allrecords = self.session.query(Rec).all()
for record in allrecords:
logging.info("R: %s " % record)
def execsql(self,sql):
records = self.conn.execute( sql )
def search(self, name="", starttime="", endtime=""):
logging.info("Looking for %s" % recfilter)
self.session.query( Rec ).filter( Rec.name=name ).first()
for row in records:
print "Found:", row

BIN
server/techrec.pyc Normal file

Binary file not shown.