【Python】连接PostgreSQL获取手机验证码

文章目录

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()

msgCode.py

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))

运行结果如下:

相关推荐
ZhengEnCi5 小时前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi6 小时前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽6 小时前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187917 小时前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L1 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅1 天前
海天线算法的前世今生
python·计算机视觉
韩师傅1 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L1 天前
LangGraph的MessageState and HumanMessage
python
韩师傅1 天前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L1 天前
python的类&继承
python