Ну вот как то так, без рефакторинга.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys # Standart library for compile source
import psycopg2 # Library for use connection with pgsql
import simplejson # Lobrary for parse string in JSON
# Server program
from socket import * # Foll import library of using and create UDP server (sock)
from psycopg2 import pool
# Set the socket parameters
host = "" # Listen any interfaces
port = 5005 # Port Listener
buf = 1500 # Size buffer for UDP listener
addr = (host,port) # VARiable of sock connection
# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM) # The create of socket connection
UDPSock.bind(addr) # and accept configure
# Create class conn
pool = psycopg2.pool.SimpleConnectionPool(1, 2, 'host=localhost dbname=logger user=log')
pool_in = psycopg2.pool.SimpleConnectionPool(1, 2, 'host=localhost dbname=logger_py user=log')
# Connector pgsql
with open('out.txt', 'a') as f: # -= Special for СКЛЕРОЗ =-
# Receive messages
while True:
data,addr = UDPSock.recvfrom(buf) # Recive the data input on host (listen up comment of 'addr') in standart stream on the UDPsock
if data: # Statement of data
res = simplejson.loads(data) # VARiable of using standart lib simplejson for parse stream in data (Recived on UDPSock)
try:
try:
con_sel = pool.getconn()
cur_sel = con_sel.cursor()
cur_sel.execute("select sid from users where wiegand26=%s", (res['CARDNUMBER'], ))
sid = cur_sel.fetchone()[0]
finally:
pool_in.putconn(con)
con = pool_in.getconn()
cur = con.cursor()
cur.execute("insert into logger_py(point, datatime, event_id, cardnumber, firstname, lastname, dep, nick) values (%s, %s, %s, %s, %s, %s, %s, %s)", (res['POINT'], res['DATATIME'], sid, res['CARDNUMBER'], res['FIRSTNAME'], res['LASTNAME'], res['DEP'], res['MIDDLENAME']))
# This is string of request in database with 8(eight row) and 8(eight) Values,
# Which take the value of the parse content JSON
con.commit() # Send request after check on exploite or injection, and close connection
except Exception as e:
print "-=-=-=-=",e
import traceback
traceback.print_exc()
f.write("\nReceived message '") # -= параллельно пишем =-
f.write(data) # -= полученные даныне =-
f.write("' ;") # -= в файл в каталоге =-
finally:
pool.putconn(con_sel)
print "\nReceived message '", data,"'" # DEBUG info
"""
while True:
data,addr = UDPSock.recvfrom(buf) # Recive the data input on host (listen up comment of 'addr') in standart stream on the UDPsock
if data: # Statement of data
res = simplejson.loads(data) # VARiable of using standart lib simplejson for parse stream in data (Recived on UDPSock)
try:
cur.execute("insert into logger_py(point, datatime, event_id, cardnumber, firstname, lastname, dep, nick) values (%s, %s, %s, %s, %s, %s, %s, %s)", (res['POINT'], res['DATATIME'], res['EVENTID'], res['CARDNUMBER'], res['FIRSTNAME'], res['LASTNAME'], res['DEP'], res['MIDDLENAME']))
# This is string of request in database with 8(eight row) and 8(eight) Values,
# Which take the value of the parse content JSON
con.commit() # Send request after check on exploite or injection, and close connection
except Exception:
f = open('out.txt', 'a')
f.write("\nReceived message '") # -= параллельно пишем =-
f.write(data) # -= полученные даныне =-
f.write("' ;") # -= в файл в каталоге =-
f.close()
print "\nReceived message '", data,"'" # DEBUG info
"""
# Close socket
UDPSock.close()