如何在python中检查连接是否存在?

时间:2014-11-04 19:11:41

标签: python python-3.x

我在raspberry pi上使用python并使用阅读器RDM880制作项目读取RFID标签。

我的想法是花时间和时间来检查工作人员是否按时上班。

我尝试使用python将card_ID,time_in,time_out添加到本地mysql和远程mysql(IP:192.168.137.1)。

它在远程和本地mysql中具有相同的表。

如果mysql远程被破坏,我只想添加到本地mysql。

这是我的代码:

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 个答案:

答案 0 :(得分:3)

您有几个选择:

(不太好)定期ping服务器以保持连接活动。

(最好)通过重新建立连接并再次尝试呼叫来调用cur.execute时处理MySQLdb异常。 Here's an excellent and concise answer for how to do just that。从那篇文章中,您自己处理异常:

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

(最后)在实际调用数据库条目之前建立新的数据库连接。在这种情况下,请将dbdb_local定义移到您在cursor之前调用的函数中。如果您正在进行数千次查询,那么这不是最好的。但是,如果它只是几个数据库查询,它可能没问题。

答案 1 :(得分:3)

我使用以下方法:

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

答案 2 :(得分:0)

我使用了一种简单的技术。最初,我使用以下方法连接到数据库:

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

每当需要检查数据库是否仍处于连接状态时,我都会使用以下一行:

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

这将检查数据库连接是否仍然有效。如果没有,它将重新启动连接以解决问题。