#-*-coding: utf-8-*-
import uuid
class coupons_N_keys:
def __init__(self):
self.num = 0;
self.list = []
def coupons(self, num):
for i in range(num):
self.list.append(uuid.uuid1())
def return_list(self):
return self.list;
test = coupons_N_keys()
test.coupons(200)
keys = test.return_list()
print(keys)
print(len(keys))
def writeToText(list_name, file_path):
try:
f = open(file_path, 'w')
for item in list_name:
f.write(str(item) + '\n\n')
f.close()
except IOError:
print('write file fail')
writeToText(keys, 'coupons.txt')
生成的激活码:
#-*-coding: utf-8-*-
import redis
class save_coupons_to_redis:
def __init__(self, path):
self.path = path
print(self.path)
def __connect(self):
try:
r = redis.StrictRedis(
host='127.0.0.1',
port=6379,
db=0
)
print(r)
return r
except IOError:
print('link redis error')
def save_to_redis(self):
r = self.__connect()
path = self.path
with open('coupons.txt', 'r') as f:
r.set('coupons', f.readlines())
r.save()
def see_all(self):
r = self.__connect()
value = r.get('coupons')
print(value)
test = save_coupons_to_redis('coupons.txt')
test.save_to_redis()
test.see_all()
运行结果:
#-*-coding: utf-8-*-
import pymysql
class save_coupons_to_mysql:
def __init__(self, path):
self.path = path
print(self.path)
def __connect(self):
try:
# 创建连接
conn = pymysql.Connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='123456',
db='python',
charset='utf8'
)
print(conn)
return conn
except IOError:
print('link mysql error')
def save_to_mysql(self):
conn = self.__connect()
path = self.path
#创建游标
cursor = conn.cursor()
cursor.execute('drop table if exists coupons_keys')
cursor.execute('create table coupons_keys (id int(8) primary key, coupons_keys varchar(50))')
row = 0
with open('coupons.txt', 'r') as f:
for line in f.readlines():
coupons_keys = line.rstrip()
cursor.execute('insert into coupons_keys (id, coupons_keys) values (%s, %s)', [row, coupons_keys])
row += 1
conn.commit()
cursor.close()
conn.close()
def see_all(self):
conn = self.__connect()
cursor = conn.cursor()
cursor.execute('select * from coupons_keys')
# 获取剩余结果的第一行数据
# row_1 = cursor.fetchone()
# 获取剩余结果前n行数据
# row_2 = cursor.fetchmany(3)
# 获取剩余结果所有数据
# row_3 = cursor.fetchall()
values = cursor.fetchall()
print(values)
cursor.close()
conn.close()
test = save_coupons_to_mysql('coupons.txt')
test.save_to_mysql()
test.see_all()
运行结果:
网友评论