Do you not give up after the introduction of the Python fairy?

Source: Internet
Author: User
Tags sha1

Not to study for the purpose of learning, it is bullying ~ So the rogue has become rich ...

Well, it's not poverty. From one day onwards, the blog only talk about learning and feelings, do not talk about work, do not pull useless ~

What's the whole day? Let me think about ~ Well, yesterday did not complete the database, then start from here, to paste a small code, small cool suddenly ~

"" "Database Basic Operation" "

From pymysql Import *
From hashlib Import *
From Pymongo Import *
From Redis Import *


def register ():
"" "Register" ""
Try
# 1. Get Connection Object
conn = Mysql_conn ()
# 2. Database Operations Object
cur = conn.cursor ()
# 3. Writing SQL
Select_params = [Uname]
Select_sql = ' Select Upwd from momo_users where uname =%s '
# 4. Execute SQL
Cur.execute (Select_sql, Select_params)
res = Cur.fetchone ()
Print (RES)

If res is not None:
Print (' User name has been registered, registration failed ')
Return

# Registered user user must not exist insert data
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 (' Registration failed ')
Else
Print (' Registered success ', uname)
# 5. If the data update operation requires a commit
Conn.commit ()
# 6. Close cursor objects and connection objects
Cur.close ()
Conn.close ()
Except Exception as E:
Print (e)

def mysql_login ():
"" "Login" "
Try:
# 1. Get Connection object
Conn = Mysql_conn ()
# 2. Data Library Action Object
cur = conn.cursor ()
# 3. Writing SQL
Select_params = [uname]
Select_sql = ' Select Upwd from Momo_users whe Re uname =%s '
# 4. Execute SQL
Cur.execute (select_sql, select_params)
res = Cur.fetchone ()
If res is None:
# The user name is incorrect
print (' username error, Login failed ')
Cur.close ()
Conn.close ()
Return
# Get password information to determine if the password is equal
Print (res)
M_pwd = res[0]
If m_pwd = = sha_pwd:
Print (' password correct, login successful ', uname)
# Store The results of the query into Mongo/redis
# col . Insert_one ({' username ': uname, ' Password ': sha_pwd})
R_client.set (uname,sha_pwd)
Else:
Print (' Password error, Login failed ')
# 6. Close the Cursor object and connection object
Cur.close ()
Conn.close ()
except Exception as E:
Print (e)


Def mongo_login ():
# Get the MONGO Connection object
M_client = mongoclient (host= ' localhost ',
PORT=27017)
db = M_client. Qqdb
Col = db. Qq_users
# The way data is stored in the data contract MONGO according to the user name {' username ': uname, ' Password ': sha_pwd}
res = Col.find_one ({' username ': uname})
If Res is None:
Print (' MONGO does not get any data in MySQL to log in ')
Mysql_login ()
Else
Print (RES)
M_password = res[' password ']
if M_password = = Sha_pwd:
Print (' Password correct, login successful, MongoDB ')
Else
Print (' Password error, Login failed, MongoDB ')


Def mysql_conn ():
Return connect (host= ' localhost ',
User= ' Root ',
password= ' MySQL ',
Database= ' Python_restore ',
port=3306,
charset= ' UTF8 ')


if __name__ = = ' __main__ ':
uname = input (' Please enter user name: ')
Upwd = input (' Please enter password: ')
# SHA1
S1 = SHA1 ()
S1.update (Upwd.encode ())
Sha_pwd = S1.hexdigest ()
Print (SHA_PWD)

# Register ()
# login ()

R_client = Strictredis (host= ' 192.168.85.70 ')
# agreed on how to store Data key value uname sha_pwd
res = R_client.get (uname)
Print (RES)
If Res is None:
Print (' No data obtained in Redis, login in MySQL ')
Mysql_login ()
Else
R_pwd = Res.decode ()
if r_pwd = = Sha_pwd:
Print (' Password correct, login successful, Redis ')
Else
Print (' Password error, Login failed, Redis ')

OK, let's get here first. The next time the python in the relevant server this code to do a ~ Yes.

Do you not give up after the introduction of the Python fairy?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.