tornado的基础项目

玩了django不过瘾怎么办,给你tornado的基本项目

目录,整理了一天的成果
这里附加上数据库中查询名字的操作,自己写的,不高效,但可以用,low爆了,但是是自己的思路,所以分享出来。

  • project
    • .idea
    • pycache
    • ORM(需要你有牢固的数据库知识去加强ORM映射)
      • init_.py
      • orm.py(不完善)
      • YJSMysql.py
    • static(静态文件)
      • css
      • font
      • html
        • index.html(你的主页,类比百度)
      • img
      • js
    • templates(模板)
    • upfile(上传文件)
    • views(类比django叫视图,这里是handler)
    • application.py(类比django叫urls.py)
    • config.py(配置,类比django叫setting.py)
    • models.py(模型,数据库的表名一定要与模型类名相同)
    • server.py(开启服务运行 python server.py 即可,配置默认绑定localhost:8000)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def loginSucess(username, password):
flag = 0
userinfolist = UserInfo.all()
# 判断名字是否存在
# 存在则返回True,不存在返回False
def nameExist(name):
flag = 0
sqlUserNames=[]
for man in userinfolist:
sqlUserNames.append(man['username'])
if name in sqlUserNames:
flag = True
else:
flag = False
return flag
# 名字存在后再进行密码确认
if nameExist(name):
# 获得正确的元组(name,password),(name,password),(name,password)
NPList = []
for man in userinfolist:
# 此处字典无序列表
t = {man['username']:man['password']}
NPList.append(t)
for body in NPList:
boolList = []
for k,v in body.items():
if pwd == body[k]:
boolList.append(1)
if 1 in boolList:
flag=True
else:
# 密码输入错误处理
info='密码输入错误'
pass
return flag
else:
# 用户名不存在处理
info='用户名不存在'
self.redirect('/login')
return False

在登陆注册可能要用到验证码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class GetVerifyCodeHandler(RequestHandler):
def get(self,*args,**kwargs):
# import VerifyCode
import io
# 引入绘图模块
from PIL import Image, ImageDraw, ImageFont
# 引入随机数模块
import random
# 定义变量,用于画面的背景色,宽,高
bgcolor = (random.randrange(20, 100), random.randrange(20, 100), random.randrange(20, 100))
width = 100
height = 50
# 创建画面对象
im = Image.new('RGB', (width, height), bgcolor)
# 创建画笔对象
draw = ImageDraw.Draw(im)
# 调用画笔的point()函数绘制噪点
for i in range(0, 100):
xy = (random.randrange(0, width), random.randrange(0, height))
fill = (random.randrange(0, 255), 255, random.randrange(0, 255))
draw.point(xy, fill=fill)
# 定义验证码的备选值
str = '1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm'
# 随机选取4个值作为验证码
rand_str = ''
for i in range(0, 4):
rand_str += str[random.randrange(0, len(str))]
# 构造字体对象
# font = ImageFont.truetype(r'C:\Windows\Fonts\9PX3BUS(1).TTF', 20)
font = ImageFont.truetype(r'static/font/AMRIGOB.TTF', 20)
# 构造字体颜色
fontcolor1 = (255, random.randrange(0, 255), random.randrange(0, 255))
fontcolor2 = (255, random.randrange(0, 255), random.randrange(0, 255))
fontcolor3 = (255, random.randrange(0, 255), random.randrange(0, 255))
fontcolor4 = (255, random.randrange(0, 255), random.randrange(0, 255))
# 绘制4个字
draw.text((5, 2), rand_str[0], font=font, fill=fontcolor1)
draw.text((25, 10), rand_str[1], font=font, fill=fontcolor2)
draw.text((50, 20), rand_str[2], font=font, fill=fontcolor3)
draw.text((75, 15), rand_str[3], font=font, fill=fontcolor4)
# print(rand_str)
# 释放画笔
del draw
self.set_cookie('code',rand_str)
buf = io.BytesIO()
im.save(buf, 'png')
self.set_header("content-type", "image/png")
self.write(buf.getvalue())

中正工作室

torndo资源

百度云

Contents
  1. 1. 玩了django不过瘾怎么办,给你tornado的基本项目
  2. 2. torndo资源
|
本站总访问量 载入天数...载入时分秒...