一、Redis
1.Redis数据库
Redis内存数据库 -> KV数据库
set username hellokitty
get username
2.Redis持久化数据
Redis提供了两种持久化数据的方案:
RDB -> 周期性将内存数据写入dump文件
AOF -> Append Only File - 把执行过的命令保存起来
memcached
Redis - 缓存(热点数据)/ 队列
二、Python连接Redis
1.存储对象
"""
序列化 -> 把对象转换成字节或者字符序列 - 串行化 - serialize
反序列化 -> 将字节或者字符序列还原成对象 - 反串行化 -deserialize
json模块 -> 字符串形式(字符)的序列化和反序列化
dumps:将字典或者列表转成JSON字符串
loads:将JSON字符串转换成字典或者列表
pickle模块 -> 字节形式的序列化和反序列化
"""
import pickle
import redis
class Student(object):
def __init__(self, name, age):
self.name = name
self.age = age
def main():
client = redis.StrictRedis(host='47.107.175.127',
password='123456')
client.set('username', 'hellokitty', 120)
print(client.ttl('username'))
print(client.get('username'))
print(client.expire('username', 300))
print(client.ttl('username'))
stu1 = Student('lyh', 20)
"""
client.hset('stu', 'name', stu1.name)
client.hset('stu', 'age', stu1.age)
"""
client.set('stu', pickle.dumps(stu1))
if __name__ == '__main__':
main()
2.获取对象
import pickle
import redis
from test1 import Student
def main():
client = redis.StrictRedis(host='47.107.175.127',
password='123456')
stu = pickle.loads(client.get('stu'))
print(stu.name)
print(stu.age)
if __name__ == '__main__':
main()
3.存储二进制文件
import base64
import redis
from test1 import Student
def main():
client = redis.StrictRedis(host='47.107.175.127',
password='123456')
with open('touxiang.jpg', 'rb') as f:
photo = base64.b64encode(f.read())
client.set('photo', photo)
if __name__ == '__main__':
main()
4.获取二进制文件
import base64
import redis
from test1 import Student
def main():
client = redis.StrictRedis(host='47.107.175.127',
password='123456')
with open('C:\\Users\\DELL\\Desktop\\ywl.jpg', 'wb') as f:
photo = client.get('photo')
f.write(base64.b64decode(photo))
if __name__ == '__main__':
main()
三、第二阶段总结
1.网络应用的模式
网络应用的模式:C/S、B/S、P2P
client-server
browser-server
peer-to-peer
移动端(手机)/PC端(浏览器) + Web后端
HTML + CSS + JavaScript --> 前端(前端展示的数据是由后端的程序提供的)
后端 ===> 运算 + 存储 ===> 用Python程序为前端提供数据和服务
Django + Flask
2.前端知识
1.CSS - 选择器 / 优先级 / 盒子模型
2.JavaScript - ES / BOM(window) / DOM(document)
document
- createElement / appendChild / insertBefore
- removeChild
- innerHTML / textContent
- getElementById / getElementsByClassName / getElementsByTagName / querySelector / querySelectorAll
Ajax - jQuery
- $.ajax({})
- $.getJSON(url, function() {})
3.Linux
- 常用命令(文件操作/权限/工具/进程/网络)
- 安装软件(yum / 源代码构建安装)
- vim使用
4.数据库
- 关系型 - MySQL - SQL(DDL / DML / DCL)
- select - 投影/别名/筛选/模糊/排序/分组/聚合函数/分页/子查询/连接查询
- NoSQL - Redis - 缓存
四、通讯录
import pymysql
class Person(object):
def __init__(self, name, tel, sex, addr, remark):
self.name = name
self.tel =tel
self.sex = sex
self.addr = addr
self.remark = remark
def __str__(self):
return f'{self.name}\t{self.tel}\t{self.sex}\t\t{self.addr}\t{self.remark}\t'
def system_page():
print('\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
print('+ +')
print(' 欢迎来到通讯录 ')
print('+ 1. 添加 +')
print('+ 2. 查询 +')
print('+ 3. 删除 +')
print('+ 4. 修改 +')
print('+ 5. 退出 +')
print('+ +')
print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
def add_person(cursor):
name = input('请输入姓名:')
tel = input('请输入电话号码:')
if len(tel) != 11:
print('电话号码只能是11位,请重新输入!')
tel = input('请输入电话号码:')
sex = input('请输入性别:')
if sex != '男' and sex != '女':
print('性别只能是男或女,请重新输入!')
sex = input('请输入性别:')
addr = input('请输入联系地址:')
remark = input('请输入备注:')
result = cursor.execute('insert into addr_book(name, tel, sex, addr, remark) values(%s,%s,%s,%s,%s)', (name, tel, sex, addr, remark))
if result == 1:
print('添加成功!')
else:
print('添加失败!')
def query_person(cursor):
name = input('请输入姓名:')
cursor.execute('select name, tel, sex, addr, remark from addr_book where name=%s ', (name,))
persons = cursor.fetchall()
print('姓名\t联系电话\t性别\t联系地址\t\t备注\t')
for person in persons:
person = Person(**person)
print(person)
def del_person(cursor):
name = input('请输入姓名:')
result = cursor.execute('delete from addr_book where name=%s', (name,))
if result == 1:
print('删除成功!')
else:
print('删除失败!')
def update_person(cursor):
name = input('请输入姓名:')
tel = input('请输入新号码:')
addr = input('请联系地址:')
result = cursor.execute('update addr_book set tel=%s, addr=%s where name=%s', (tel, addr, name))
if result == 1:
print('修改成功!')
else:
print('修改失败!')
def main():
con = pymysql.connect(host='localhost',
port=3306,
user='root',
passwd='123456',
database='abs',
charset='utf8',
autocommit=True,
cursorclass=pymysql.cursors.DictCursor
)
try:
with con.cursor() as cursor:
system_page()
operate = input('请选择操作:')
if operate == '1':
add_person(cursor)
elif operate == '2':
query_person(cursor)
elif operate == '3':
del_person(cursor)
elif operate == '4':
update_person(cursor)
elif operate == '5':
exit()
except pymysql.MySQLError as e:
print('操作失败!', e)
finally:
con.close()
if __name__ == '__main__':
main()