zoukankan      html  css  js  c++  java
  • python仙修之 入门之后不放弃?

            不以学习为目的的学习,那是耍流氓~所以流氓都成了有钱人。。。。。。

            额,不贫了。从某天开始,次博客只谈学习和感情,不谈工作,不扯没用的~

            今天整个啥呢?容我思量思量~嗯,昨天不整了数据库吗,那就从这里开始吧,来粘贴个小代码,小爽一下子~

                    “”“数据库の基本操作”“”    

    from pymysql import *
    from hashlib import *
    from pymongo import *
    from redis import *


    def register():
    """注册"""
    try:
    # 1. 获取连接对象
    conn = mysql_conn()
    # 2. 数据库操作对象
    cur = conn.cursor()
    # 3. 编写sql
    select_params = [uname]
    select_sql = 'select upwd from momo_users where uname = %s'
    # 4.执行sql
    cur.execute(select_sql, select_params)
    res = cur.fetchone()
    print(res)

    if res is not None:
    print('用户名已经被注册,注册失败')
    return

    # 注册用户 用户一定不存在 插入数据
    insert_params = [uname, sha_pwd]
    insert_sql = 'insert into momo_users (uname,upwd) values (%s,%s)'
    count = cur.execute(insert_sql, insert_params)
    if count == 0:
    print('注册失败')
    else:
    print('注册成功', uname)
    # 5. 如果是数据更新操作需要commit
    conn.commit()
    # 6.关闭cursor对象和连接对象
    cur.close()
    conn.close()
    except Exception as e:
    print(e)

    def mysql_login():
    """登陆"""
    try:
    # 1. 获取连接对象
    conn = mysql_conn()
    # 2. 数据库操作对象
    cur = conn.cursor()
    # 3. 编写sql
    select_params = [uname]
    select_sql = 'select upwd from momo_users where uname = %s'
    # 4.执行sql
    cur.execute(select_sql, select_params)
    res = cur.fetchone()
    if res is None:
    # 根据用户名没有获取到相关信息,此时用户名错误
    print('用户名错误,登录失败')
    cur.close()
    conn.close()
    return
    # 获取密码信息 判断密码是否相等
    print(res)
    m_pwd = res[0]
    if m_pwd == sha_pwd:
    print('密码正确,登录成功',uname)
    # 将查询到的结果存储到mongo/redis中
    # col.insert_one({'username':uname,'password':sha_pwd})
    r_client.set(uname,sha_pwd)
    else:
    print('密码错误,登录失败')
    # 6.关闭cursor对象和连接对象
    cur.close()
    conn.close()
    except Exception as e:
    print(e)


    def mongo_login():
    # 获取mongo的连接对象
    m_client = MongoClient(host='localhost',
    port=27017)
    db = m_client.QQDB
    col = db.QQ_users
    # 根据用户名获取数据 约定mongo中的数据存储的方式 {'username':uname,'password':sha_pwd}
    res = col.find_one({'username': uname})
    if res is None:
    print('mongo中没有获取到任何数据,在mysql中完成登陆')
    mysql_login()
    else:
    print(res)
    m_password = res['password']
    if m_password == sha_pwd:
    print('密码正确,登录成功,mongodb')
    else:
    print('密码错误,登录失败,mongodb')


    def mysql_conn():
    return connect(host='localhost',
    user='root',
    password='mysql',
    database='python_restore',
    port=3306,
    charset='utf8')


    if __name__ == '__main__':
    uname = input('请输入用户名:')
    upwd = input('请输入密码:')
    # sha1
    s1 = sha1()
    s1.update(upwd.encode())
    sha_pwd = s1.hexdigest()
    print(sha_pwd)

    # register()
    # login()

    r_client = StrictRedis(host='192.168.85.70')
    # 约定好如何存储数据 key value uname sha_pwd
    res = r_client.get(uname)
    print(res)
    if res is None:
    print('redis中没有获取到任何数据,在mysql中完成登陆')
    mysql_login()
    else:
    r_pwd = res.decode()
    if r_pwd == sha_pwd:
    print('密码正确,登录成功,redis')
    else:
    print('密码错误,登录失败,redis')

     

             好了,就先到这里吧。下一次把Python中相关的服务器这块儿代码搞一搞~可以的。

    Donglonglong
  • 相关阅读:
    第六章 (3)CreateThread函数
    第六章(5)C/C++运行期库
    自己去除迅雷广告
    第六章(4)终止线程的运行
    第四章 进程(7)CreateProcess函数详解
    第六章(6)进程ID的相关函数
    第六章 线程的基础知识
    第四章 进程(5)进程的当前驱动器和目录
    第四章 进程(6)CreateProcess函数详解
    第六章 (2)线程函数
  • 原文地址:https://www.cnblogs.com/dll-123/p/7398705.html
Copyright © 2011-2022 走看看