Python Fun Exercise-Save the activation code to MySQL and Redis

Source: Internet
Author: User
Tags uuid

Keep doing the exercises today, the topic is https://github.com/Yixiaohan/show-me-the-code.



question No. 0001: as an independent developer of the Apple Store App, do you want to make a limited-time promotion, generate an Activation code (or coupon) for your app, and use Python to generate 200 activation codes (or coupons)?

question No. 0002: Save the 200 activation codes (or coupons) generated by the 0001 questions to the MySQL relational database.

question No. 0003: Save the 200 activation codes (or coupons) generated by 0001 questions to the Redis non-relational database.


The implementation code is as follows:

Generate Activation code module UUID, here can use uuid1,4,5 and so on, the main difference is different encryption algorithm and generation factor, I use uuid4 here, is purely random

The operation of the database I realized through Pymysql. Basic operation can refer to http://beanxyz.blog.51cto.com/5570417/1871039

The operation of Redis is achieved through the Redis module. Basic operation can refer to http://beanxyz.blog.51cto.com/5570417/1870139


#! /usr/bin/env python#! -*- coding:utf-8 -*-# author: yuan liimport  Uuidimport pymysqlimport redisdef generate_key (num):    li=[]     for i in range (num):         #uuid4是随机生成的验证码         s = uuid.uuid4 ()          valuekey = str (S)         li.append (Valuekey)     return li    def save_db (keys): #绑定数据库和表      conn = pymysql.connect (host= ' Sydnagios ',  port=3306, user= ' Yli ',  passwd= ' Yli ',  db= ' test ')      #通过游标来执行sql语句     cursor =  Conn.cursor (cursor=pymysql.cursors.dictcursor)     for valuekey in keys:     #  inserting values into the table        cursor.execute ("Insert into code (serial)  value   (%s) ",  (Valuekey))     conn.commit ()     #  Query the results of the table     cursor.execute (' select * from code ')     #  output results     row = cursor.fetchall ()     for item in  Range (len):         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 (    save_db) (LI)     save_redis (LI) 


The effect of executing the script:

Control Desk

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/A6/FF/wKioL1ne-o2yJhZZAABC7huunIU285.png "style=" float : none; "title=" 5.PNG "alt=" Wkiol1ne-o2yjhzzaabc7huuniu285.png "/>


Redis Cache

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/08/48/wKiom1ne_USRjueTAABCWVAs6M4663.png "style=" float : none; "title=" 6.PNG "alt=" Wkiom1ne_usrjuetaabcwvas6m4663.png "/>


MySQL Database

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/A6/FF/wKioL1ne-o2TRPenAAA3FYU7vVA513.png "style=" float : none; "title=" 7.PNG "alt=" Wkiol1ne-o2trpenaaa3fyu7vva513.png "/>


This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1971685

Python Fun Exercise-Save the activation code to MySQL and Redis

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.