Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python to connect and save the activation code to mysql and redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Let's learn about Python to achieve connection and save the activation code to mysql and redis, I believe you will benefit a lot after reading, the text in the essence is not much, hope Python to achieve connection and save the activation code to mysql and redis this short content is what you want.

The code for the implementation is as follows:

The module uuid that generates the activation code, uuid1,4,5 can be used here, and so on. The main difference is different encryption algorithms and generating factors. I use uuid4 here, which is purely random.

I implement the operation of the database through pymysql. For basic operations, please refer to http://beanxyz.blog.51cto.com/5570417/1871039.

The operation of redis is implemented through the redis module. For basic operations, please refer to http://beanxyz.blog.51cto.com/5570417/1870139.

#! / usr/bin/env pythonium!-*-coding:utf-8-*-# Author: Yuan Liimport uuidimport pymysqlimport redisdef generate_key (num): li= [] for i in range (num): # uuid4 is a randomly generated CAPTCHA S = uuid.uuid4 () valuekey = str (S) li.append (valuekey) return li def save_db (keys): # bind database and table conn = pymysql.connect (host='sydnagios' Port=3306, user='yli', passwd='yli', db='test') # execute sql statements through cursors cursor= conn.cursor (cursor=pymysql.cursors.DictCursor) for valuekey in keys: # insert values into the table cursor.execute ("insert into Code (serial) value (% s)" (valuekey)) conn.commit () # query the result of the table cursor.execute ('select * from Code') # output result row = cursor.fetchall () for item in range (len (row)): print (row [item]) cursor.close () conn.close () import redisdef save_redis (keys): r = redis.Redis (host='sydnagios') Port=6379) for item in keys: r.lpush ('list1',item) if _ _ name__ = =' _ main__': li=generate_key (20) save_db (li) save_redis (li)

The effect of executing the script:

Console

Redis caching

Mysql database

After reading the article about Python connecting and saving activation codes to mysql and redis, many readers will want to know more about it. For more industry information, you can follow our industry information section.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report