I wrote a script in python for listening on a TCP port and receiving data from remote device and saves it in database, and also I use respawn for running this script in background and continuously, but when remote device connected to TCP Port for first time, the script gets data and saves them in database correctly, but after a while that remote device disconnected from port and again connected to the port, after this time, script does not save data in data base, how ever remote device can connect to port with no problem. this is the python code:
from socket import *
import MySQLdb
TCP_IP = '*.*.*.*'
TCP_PORT = 5005
BUFFER_SIZE = 1024
db = MySQLdb.connect("localhost","***","****","***" )
sock=socket()
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 100)
sock.bind((TCP_IP, TCP_PORT))
sock.listen(10)
conn, addr = sock.accept()
print 'Connection address:', addr
try:
while True:
data = conn.recv(BUFFER_SIZE)
cursor = db.cursor()
sql = "INSERT INTO data(DATA) VALUES ('%s')" % (data)
cursor.execute(sql)
db.commit()
if not data: break
print "received data:", data
conn.send(data)
conn.close()
db.close()
except:
db.rollback()
what is the problem?