Python Socket Mysql
ismi lazım olmayan bir projede kullandığım network dinleme string parse ve mysql update işlemlerini yapan python
da kendi ufak görevi büyük python kodum.
Öğrenci arkadaşlara örnek olması dileğiyle..
#!/usr/bin/python
import MySQLdb
import socket
from random import randint
import os
from array import *
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="root",
passwd="pass",
db="db")
UDP_IP = "192.168.1.15"
UDP_PORT = 5000
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
datam=list()
i=1
veri = ""
while True:
data, addr = sock.recvfrom(2048) # buffer size is 1024 bytes
veri=repr(data)
# print data
if veri.__contains__("MDL"):
veri=veri.replace('MDL', '')
veri=veri.replace('\'', '')
mdl = veri.split(";")
kontrol_uzunluk=len(mdl)
if kontrol_uzunluk==101:
for m in mdl:
rastgele=randint(10, 30)
mdl2 = m.split(",")
if len(mdl2) == 3:
for m2 in mdl2:
datam.append(m2)
if len(datam)==3:
os.system('clear')
with db:
cur = db.cursor()
cur.execute("UPDATE modules SET temp = %s,state=%s WHERE id = %s",
#(rastgele,i))
(datam[2],datam[1], i))
print "\rguncellendi ->"+str(i)+" sicaklik"+str(datam[2])
else:
print 'hatali veri row boyutu'
datam = list()
i += 1
datam=list()
else:
print 'hatali veri main boyutu'
print kontrol_uzunluk
i=0
# print "bekliyo"