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

运行结果如下:

相关推荐
光影6271 小时前
Python接口自动化测试----Requests库基础入门
开发语言·python·测试工具·pycharm·自动化
ch.ju1 小时前
Java Programming Chapter 4——Inherited call
java·开发语言
Kobebryant-Manba1 小时前
学习参数管理
pytorch·python·深度学习
是有头发的程序猿1 小时前
竞品分析 + 用户洞察自动化|基于 item_review 评论接口 + 多 AI Agent 实现淘宝评论全量采集与智能分析(附python源码)
java·python·自动化
信看1 小时前
Jetson Orin Quectel QMI 拨号上网
开发语言·python
keykey6.1 小时前
LSTM 文本情感分析:从词嵌入到分类实战
开发语言·人工智能·深度学习·机器学习
数据知道1 小时前
网站到底是如何通过JS读取你的浏览器指纹的?
开发语言·javascript·ecmascript·指纹浏览器
骑士雄师1 小时前
课程导航LangGraph核心概念
python
c238561 小时前
C++的IO流深入理解(上)
开发语言·c++