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

运行结果如下:

相关推荐
神仙别闹2 小时前
基于 C++ 实现(控制台)学生成绩管理系统
开发语言·c++
Y幽谷客2 小时前
Python加载本地大模型(Qwen3.5 8B)
python·大模型
伞伞悦读2 小时前
【第36期】Python 目录与路径详解:pathlib、文件遍历、创建、复制、移动和删除风险
开发语言·python
线上放牧人3 小时前
Windows删除图标缓存
windows·python·pyqt
qq_5470261793 小时前
Python 变量和简单数据类型
python
智搜广告4 小时前
智搜广告:科技行业AI回答优化公司如何破局
大数据·python·elasticsearch·geo
IpdataCloud5 小时前
AI智能体调用工具怎么核验来源IP?归属地、网络类型与代理风险识别(含Python代码)
数据库·python·tcp/ip
2601_966949655 小时前
只拿到股票代码还不够:量化程序到底还需要哪些标的信息?——从数据建模开始理解股票元数据
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
蒸鱼Yuzheng5 小时前
Python 游戏测试开发怎么准备:日志解析、接口校验、并发与可维护性
自动化测试·python·测试开发·面试题·游戏测试
m0_734571765 小时前
深入理解5g <十八>传输块tbsize的计算
开发语言·5g