文章目录
do_psycopg2.py
python
import psycopg2
class DoPsycopg2(object):
def __init__(self, host, port, user, password, database):
self.db = psycopg2.connect(
host=host,
port=port,
user=user,
password=password,
database=database
)
self.cursor = self.db.cursor()
def execute(self, query, args=None):
self.cursor.execute(query, args)
def commit(self):
self.db.commit()
def fetchall(self):
return self.cursor.fetchall()
def fetchone(self):
return self.cursor.fetchone()
def rowcount(self):
return self.cursor.rowcount
def close(self):
self.cursor.close()
self.db.close()
python
from do_psycopg2 import DoPsycopg2
import re
#
db_sys_message = DoPsycopg2(
host='172.30.206.xx',
port=5432,
user='postgres',
password='password',
database='sys-message'
)
def query_sms(phone):
sql = 'select content from sys_sms_record where phone=%s order by create_time desc limit 1;'
db_sys_message.execute(sql, (str(phone),))
row = db_sys_message.fetchone()
content = row[0]
print("短信内容:", content)
code = re.findall('您的验证码为(\d*)', content)[0]
db_sys_message.commit()
return code
if __name__ == '__main__':
print(query_sms(15808621723))
运行结果如下:
