How can I check the connection in python?

I am doing a project reading an RFID tag using python on a raspberry pi and using an RDM880 reader.

My idea is to take the time and time to check the work of the staff on time or not.

I am trying to add card_ID, time_in, time_out to local mysql and remote mysql (IP: 192.168.137.1) using python.

It has the same table in remote and local mysql.

If the mysql remote is corrupted, I only want to add the local mysql.

Here is my code:

import serial
import time
import RPi.GPIO as GPIO
import MySQLdb
from datetime import datetime
from binascii import hexlify
serial=serial.Serial("/dev/ttyAMA0",                    
                  baudrate=9600,                     
                  parity=serial.PARITY_NONE,                    
                  stopbits=serial.STOPBITS_ONE,                    
                  bytesize=serial.EIGHTBITS,
                  timeout=0.1) 
db_local = MySQLdb.connect("localhost","root","root","luan_van") #connect local
db = MySQLdb.connect("192.168.137.1", "root_a","","luan_van") #connect remote
ID_rong = 128187 # reader respone if no card
chuoi= "\xAA\x00\x03\x25\x26\x00\x00\xBB"
def RFID(str): #function read RFID via uart 
      serial.write(chuoi)
      data = serial.readline()
      tach_5 = data[5]
      tach_6 = data[6]
      hex_5 = hexlify(tach_5)
      hex_6= hexlify(tach_6)
      num_5 = int(hex_5,16)
      num_6 = int(hex_6,16)
      num_a = num_5 * 1000 + num_6
      if(num_a != ID_rong):
            tach_7 = data[7]
            tach_8 = data[7]
            hex_7 = hexlify(tach_7)
            hex_8= hexlify(tach_8)
            num_7 = int(hex_7,16)
            num_8 = int(hex_8,16)
            num = num_8 + num_7 * 1000 + num_6 * 1000000 + num_5 * 1000000000
      else:
            num = num_5 * 1000 + num_6
      return num
def add_database(): # add card_ID and time_in to remote mysql
      with db:
           cur = db.cursor()
           cur.execure("INSERT INTO tt_control(Card_ID,Time_in) VALUES ('%d',NOW()) " %num)
           return
def add_database_local(): # add card_ID and time_in to remote mysql
      with db_local:
           cur = db_local.cursor()
           cur.execure("INSERT INTO tt_control(Card_ID,Time_in) VALUES ('%d',NOW()) " %num)
           return
def have_ID(int): #check ID in table tt_control
       with db_local:
           cur = db_local.cursor(MySQLdb.cursors.DictCursor)
           cur.execute("SELECT * FROM tt_control WHERE Card_ID = '%d'" %num)
           rows = cur.fetchall()
           ID=""
           for row in rows:
               ID = row['Card_ID']
       return ID
def add_time_out(): #add time out to remote mysql
       with db:
           cur = db.cursor(MySQLdb.cursors.DictCursor)
           cur.execute("UPDATE tt_control SET Time_out = NOW() WHERE Card_ID = '%d'" %num)
       return
def add_time_out_local(): #add time out to local mysql
       with db_local:
           cur = db_local.cursor(MySQLdb.cursors.DictCursor)
           cur.execute("UPDATE tt_control SET Time_out = NOW() WHERE Card_ID = '%d'" %num)
       return

  def add_OUT(): #increase Card_ID to distinguish second check
     with db:
           cur = db.cursor(MySQLdb.cursors.DictCursor) 
           cur.execute("UPDATE tt_control SET Card_ID = Card_ID + 1 WHERE Card_ID = '%d'" %num)
     return
  def add_OUT_local(): #increase Card_ID to distinguish second check
     with db_local:
           cur = db_local.cursor(MySQLdb.cursors.DictCursor) 
           cur.execute("UPDATE tt_control SET Card_ID = Card_ID + 1 WHERE Card_ID = '%d'" %num)
     return
  while 1:
     num = RFID(chuoi)
     time.sleep(1)
     Have_ID =have_ID(num)
     if(num != ID_rong):
            if(Have_ID ==""):
                 add_database() #---> it will error if remote broken, how can i fix it?
                 add_database_local()
            else:
                 add_time_out() #---> it will error if remote broken, how can i fix it? I think connection alive can fix, but I don't know
                 add_time_out_local()
                 add_OUT()
                 add_OUT_local() #---> it will error if remote broken, how can i fix it?

      

+3


source to share


3 answers


You have several options:

(not so good) Ping the server regularly to keep in touch.

(best) Handle MySQLdb exception when calling cur.execute by re-establishing the connection and calling again. Here's a great and concise answer on how to do it . From this article, you handle the exception yourself:



def __execute_sql(self,sql,cursor):
    try:
        cursor.execute(sql)
        return 1
    except MySQLdb.OperationalError, e:            
        if e[0] == 2006:
            self.logger.do_logging('info','DB', "%s : Restarting db" %(e))
            self.start_database()
            return 0

      

(finally) Establish a new database connection just before you actually call the database records. In this case, move the definition db

and db_local

in the function you call just before cursor

. If you make thousands of requests, this is not the best. However, if these are just a few queries to the database, it might be a good thing.

+3


source


I am using the following method:



def checkConn(self):
    sq = "SELECT NOW()"
    try:
        self.cur.execute( sq )
    except pymysql.Error as e:
        if e.errno == 2006:
            return self.connect()
        else:
            print ( "No connection with database." )
            return False

      

+3


source


I used a simple technique. Initially, I connected to the DB using:

conect = mysql.connector.connect(host=DB_HOST, user=DB_USER, password=DB_PASS, database=DB_NAME)

      

Whenever I need to check if the DB is connected, I used the line:

conect.ping(reconnect=True, attempts=3, delay=2)

      

This will check if the DB connection is alive. If not, it will restart the connection, which will fix the problem.

0


source







All Articles