微博程序:RUN-1,Index
- c - views-routes_weibo
- m - models
- v- template
浏览器请求:
GET /weibo/index HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
服务器响应:
server-run-request拿到path用response_for_path(path)这里把path拆成path和query然后转给response = r.get(path, error)这里面路由匹配给routes_weibo里面的index方法-->然后重定向redirect('/login')把headers['Location'] login和状态码302加入到location再返回-->
响应:
b'HTTP/1.1 302 OK\r\nContent-Type: text/html\r\nLocation: /login\r\n\r\n'
浏览器请求:
GET /login HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
服务器:
run-直接转login返回网页了
b'HTTP/1.1 210 VERY OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <title>\xe6\xb3\xa8\xe5\x86\x8c\xe7\x99\xbb\xe5\xbd\x95\xe9\xa1\xb5\xe9\x9d\xa2</title>\n</head>\n<body>\n <h1>\xe7\x99\xbb\xe5\xbd\x95</h1>\n <h2>\xe4\xbd\xa0\xe5\xa5\xbd </h2>\n <form action="/login" method="post">\n <input type="text" name="username" placeholder="\xe8\xaf\xb7\xe8\xbe\x93\xe5\x85\xa5\xe7\x94\xa8\xe6\x88\xb7\xe5\x90\x8d">\n <br>\n <input type="text" name="password" placeholder="\xe8\xaf\xb7\xe8\xbe\x93\xe5\x85\xa5\xe5\xaf\x86\xe7\xa0\x81">\n <br>\n <button type="submit">\xe7\x99\xbb\xe5\xbd\x95</button>\n </form>\n <h3></h3>\n</body>\n</html>'
微博程序:RUN-1,'/weibo/new': login_required(new),
- 登录验证方法.
# 定义一个函数统一检测是否登录
def login_required(route_function):
def func(request):
uid = current_user(request)
log('登录鉴定, user_id ', uid)
if uid == -1:
# 没登录 不让看 重定向到 /login
return redirect('/login')
else:
# 登录了, 正常返回路由函数响应
return route_function(request)
return func
BUG
- 应该导入routes.session中的session是个 session = {}
from routes.session import session
*然而我导入的是
from routes import session
报错也是很明显因为我导入的是module,不是数组.
总之打印一切,立马就能验证出来bug
又是因为from routes.session import session导入错误