0

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?

2 Answers 2

1

Because your code does not has a loop(what I means is not your read data loop). When sock.accept() return, the code will run into try block, and never go back. When then connection is close, this will just stop. I think you can add a while Ture to your code to wrap sock.accept() and read data loop.

just like:

while True:
     conn, addr = sock.accept()
     try:
         while True:
            read data
            save into db
            if not data: break
     except:
         ....
     finally:
         conn.close()
Sign up to request clarification or add additional context in comments.

3 Comments

outside need a loop also,read data loop is necessary, I think.
what do you mean? I have a loop with while true,
your loop is when the connection is established, you read the incoming data in your loop. But you should notice when the connection is closed, your code can not accept again. Because your code is run to the end. you should add a loop to accept the connection again and again. Off course, only the first connection is closed, the second can be accept in your code.
1

You need move

conn, addr = sock.accept()
print 'Connection address:', addr

into while loop, so server can accept client continuosly don't forget move

conn.close()

into while loop, so can release resource correctly

1 Comment

remote device sends data every second and I think it is better that every time that remote device is on opens a connection and sends data through that connection continuously until connection closed for any reason, and again open new connection, but your idea means remote device every second open a connection send data and close connection and again 1 second later ...., what must I do in first situation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.