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

运行结果如下:

相关推荐
来了就未晚9 分钟前
Python基础 学习代码存储与命名规范
开发语言·python·学习
落魄大学生之流水线上谋生计21 分钟前
LangGraph 基本用法指南
java·windows·python·langchain
xuefuhe25 分钟前
PostgreSQL源码1:select 1
postgresql
布莱克60530 分钟前
数组详解:定义、作用、应用场景及与链表的区别(C/C++ 代码讲解)
c语言·开发语言·c++·数组
hongmai66688843 分钟前
MP2348GTL-Z:这颗24V/4A同步降压芯片,把性能和体积平衡得恰到好处
c语言·开发语言·arm开发·单片机·5g
华研前沿标杆游学1 小时前
宇树科技对外开放参访?机器人企业参访体验与行业核心价值解析
python
多弗朗皮卡丘1 小时前
C语言梦开始的地方19:文件操作
c语言·开发语言
Dovis(誓平步青云)1 小时前
折叠屏悬停看视频,上半屏和下半屏应该各做什么
android·java·服务器·开发语言·安全·音视频
今天会营业1 小时前
Miniconda安装
python·机器学习
benchmark_cc1 小时前
Python量化实战:如何检测并剔除历史数据中的“闪崩/乌龙指”异常价格点?
开发语言·人工智能·python·量化·quantdash·量化数据源