forked from aze2201/ISP_Online-Charging-System_DCC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirecAcess.py
77 lines (62 loc) · 2.13 KB
/
DirecAcess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from socket import *
import thread
import sqlite3
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('../config/config.ini')
BUFF = 1024
HOST = parser.get('DBSERVER', 'IP')
PORT = parser.get('DBSERVER', 'PORT') # must be input parameter @TODO
DATABASE="file:memdb1?mode=memory&cache=shared"
print "Server started with "+str(HOST)+" | "+src(PORT)
conn=sqlite3.connect(DATABASE ,check_same_thread=False,isolation_level=None)
def get_by_address(address):
return [x for x in globals().values() if id(x)==address]
class SQLiteDirectAccess:
def __init__(self):
self.connection=conn
print self.connection
def exct(self,data):
result=''
try:
result=conn.execute(data).fetchall()
except Exception as e:
result=e
return result
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = str(row[idx]).encode('utf8')
return d
conn.row_factory = dict_factory
def response(key):
result=''
try:
#result=conn.execute(key).fetchall()
result=MemoryClass.exct(key)
except Exception as e:
result=e
return result
#return 'Server response: ' + key
def handler(clientsock,addr):
while 1:
data = clientsock.recv(BUFF)
if not data: break
print repr(addr) + ' recv:' + repr(data)
clientsock.send(str("ChargeDB > ")+str(response(data)))
#print repr(addr) + ' sent:' + repr(response(data))
if "close" == data.rstrip(): break # type 'close' on client console to close connection from the server side
clientsock.close()
print addr, "- closed connection" #log on console
if __name__=='__main__':
ADDR = (HOST, PORT)
MemoryClass=SQLiteDirectAccess()
serversock = socket(AF_INET, SOCK_STREAM)
serversock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serversock.bind(ADDR)
serversock.listen(5)
while 1:
print 'waiting for connection... listening on port', PORT
clientsock, addr = serversock.accept()
print '...connected from:', addr
thread.start_new_thread(handler, (clientsock, addr))