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

运行结果如下:

相关推荐
编码者卢布1 小时前
【Azure APIM】APIM的诊断日志与Application Insights的日志是否可以串联为一个端到端的日志链路呢?
python·flask·azure
hold?fish:palm1 小时前
7 接雨水
开发语言·c++·leetcode
jnrjian1 小时前
\dx Postgres 查看EXTENSION
postgresql
FriendshipT1 小时前
Ultralytics:解读CBLinear模块
人工智能·pytorch·python·深度学习·目标检测
Lihua奏1 小时前
高可用与扩展:一台 PostgreSQL 不够用之后怎么办?
postgresql
geats人山人海2 小时前
c# 第九章 record
开发语言·c#
早期的虫儿有鸟吃2 小时前
vue2--Vuex 模块化
开发语言·前端·javascript
AI开发发烧友2 小时前
LangGraph多Agent协作实战:以物流售前场景为例的5 Agent工作流设计
python
码云骑士2 小时前
70-多Agent协作-CrewAI-AutoGen-角色分工与信息传递协议
python
浪客川2 小时前
Android的SystemUI的启动流程简析
android·开发语言