Какая разница где. первый комментарий, это кусок кода. И это кусок кода. Переменная определяется выще в коде.
Ну для наглядности пожалуйте:
import time
date_stmp = time.strftime("%Y-%m-%d-%H:%M")
....
while True:
data,addr = UDPSock.recvfrom(buf)
if data:
res = simplejson.loads(data)
if res.get('EVENTID', None) in ["41", "42"]:
try:
with get_conn(pool) as con_sel:
cur_sel = con_sel.cursor()
cur_sel.execute("select sid from users where wiegand26=%s ", (res['CARDNUMBER'], ))
sid = cur_sel.fetchone()[0]
cur_sel.execute("select id from device where name=%s", (res['POINT'], ))
dev_id = cur_sel.fetchone()[0]
cur_sel.execute("insert into events(user_sid, device_sid, event_type_id, event_date, add_info) values (%s, %s, %s, %s, %s)", (sid, dev_id, res['EVENTID'], res['DATATIME'], res['DEP'] ))
con_sel.commit()
except Exception as e:
with open('out.txt_'+date_stmp, 'a') as f:
# print "-=-=-=-=\n",e,"\n=-=-=-=-"
# import traceback
# traceback.print_exc()
f.write("\nReceived message '")
f.write(data)
f.write("' ;")
f.close()
# f.flush()
print "\nReceived message '", data,"'"
else:
# print("error",res.get('EVENTID', None))
print "error",res.get('EVENTID', None)