web.py session

web.py session学习:

import web
from web import form

urls = (
‘/‘,‘Index‘,
‘/test‘,‘Test‘,
‘/login‘,‘Login‘,
‘/logout‘,‘Logout‘,
)

render = web.template.render(".")

allowed = (
(‘admin‘,‘123123‘),
)

web.config.debug = False
app = web.application(urls, locals())
session = web.session.Session(app, web.session.DiskStore(‘sessions‘))

class Index:
def GET(self):
if session.get(‘logged_in‘,False):
return ‘<h1>Login Success!!!</h1><a href="/test">test</a></br><a href="/logout">Logout</a>‘
raise web.seeother(‘/login‘)

class Login:
def GET(self):
return render.login()
def POST(self):
i = web.input()
username = i.get(‘username‘)
passwd = i.get(‘passwd‘)
if (username,passwd) in allowed:
session.logged_in = True
web.setcookie(‘system_mangement‘, ‘‘, 60)
raise web.seeother(‘/‘)
else:
return ‘<h1>Login Error!!!</h1></br><a href="/login">Login</a>‘

class Logout:
def GET(self):
session.logged_in = False
raise web.seeother("/login")

class Test:
def GET(self):
if session.get(‘logged_in‘,False):
return ‘<h1> test login success!!!</h1></br><a href="/logout">Logout</a>‘
return ‘<h1>logout now</h1></br><a href="/login">Login</a>‘

if __name__ == ‘__main__‘:
app.run()

结果有:

GET http://localhost:8080/

Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/
Request Method:GET
Status Code:303 See Other
Response Headers
view source
Content-Type:text/html
Date:Fri, 07 Apr 2017 15:30:27 GMT
Location:http://localhost:8080/login
Server:localhost
Set-Cookie:webpy_session_id=c0544ca962c24b92577c1468251ff66e6d6dcc2a; Path=/; httponly
Transfer-Encoding:chunked
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive #正常情况下第一次请求是没有cookie的
Host:localhost:8080
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36

浏览器会自动重定向
GET http://localhost:8080/login
Gereral:
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/login
Request Method:GET
Status Code:200 OK

Response Headers
view source
Content-Type:text/html; charset=utf-8
Date:Fri, 07 Apr 2017 15:30:27 GMT
Server:localhost
Set-Cookie:webpy_session_id=c0544ca962c24b92577c1468251ff66e6d6dcc2a; Path=/; httponly
Transfer-Encoding:chunked

Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive
Cookie:webpy_session_id=c0544ca962c24b92577c1468251ff66e6d6dcc2a
Host:localhost:8080
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36

输入用户名密码:
POST http://localhost:8080/login {‘username’:admin,‘passwd‘:123123}
Gereral:
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/login
Request Method:POST
Status Code:303 See Other
Response Headers
view source
Content-Type:text/html
Date:Fri, 07 Apr 2017 15:33:48 GMT
Location:http://localhost:8080/
Server:localhost
Set-Cookie:system_mangement=; expires=Fri, 07 Apr 2017 15:34:48 GMT; Path=/
Set-Cookie:webpy_session_id=c0544ca962c24b92577c1468251ff66e6d6dcc2a; Path=/; httponly
Transfer-Encoding:chunked
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:28
Content-Type:application/x-www-form-urlencoded
Cookie:webpy_session_id=c0544ca962c24b92577c1468251ff66e6d6dcc2a
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/login
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Form Data
username:admin
passwd:123123

时间: 2024-10-28 11:43:38

web.py session的相关文章

Web.py session用户认证

调试一个用session来认证用户的程序 大概是这个样子 基本上可以当作webpy session认证的示例程序了 #!/usr/bin/env python #coding=utf-8 import web import time urls = ( '/', 'index', '/xml', 'pushxml', '/login', 'login', '/logout', 'logout', ) render = web.template.render('templates/') web.co

部署新浪SAE web.py Session及图片上传等问题注意事项

1.以下几条代码解决编码问题 import sysreload(sys)sys.setdefaultencoding('utf-8') 2.图片上传问题 需要开通sina的Storage服务,随便建个什么Domain,注意HTML代码,那个enctype属性一定要写,两个上传图片的控件叫avatar和photo <form action="details" method="post" enctype="multipart/form-data"

使用web.py 搭建服务器

有很多python的web框架,web.py是一个轻量级Python web框架.她并不是使用很多的一个. 但并不妨碍她的简单实用.搭建一个嵌入式web服务器最好不过. 下面把笔者搭建过程做一个介绍: 1. 下载python 2.7.8 ,做交叉编译,需要依据自己的平台做修改: ./configure make python Parser/pgen mv python python_for_build;mv Parser/pgen Parser/pgen_for_build make distc

WSGI、flup、fastcgi、web.py的关系

WSGI.flup.fastcgi.web.py的关系 Apache/lighttpd: 相当于一个request proxy,根据配置,把不同的请求转发给不同的server处理,例如静态的文件请求自己处理,这个时候它就像一个web server,对于fastcgi/python这样的请求转发给flup这样的Server/Gateway进行处理 flup: 一个用python写的web server,也就是cgi中所谓的Server/Gateway,它负责接受apache/lighttpd转发的

web.py源码分析: application

本文主要分析的是web.py库的 application.py 这个模块中的代码.总的来说, 这个模块主要实现了WSGI兼容的接口,以便应用程序能够被WSGI应用服务器调用 .WSGI是 Web Server Gateway Interface 的缩写,具体细节可以查看 WSGI的WIKI页面 接口的使用 使用web.py自带的HTTP Server 下面这个例子来自官方文档的 Hello World ,这个代码一般是应用入口的代码: import web urls = ("/.*",

使用PuTTy在CentOS下安装web.py与简单的文件传输

两周前,出于帮朋友忙的目的,尝试了一下微信公众号的菜单自定义与自动回复功能的实现,成了. 两周后,需要将代码转移至朋友新购的服务器上,发现基本操作全忘记了,麻瓜!所以记一笔,希望也能对大家也有帮助. 腾讯云买的服务器,系统为CentOS 7.2 64位,自带python2.75. 第一步,安装web.py 网上推荐的用这个 easy install 下载管理 python 的包,所以先安装 easy install: yum install python-setuptools 安装完成,输入:

py3 web.py转https://blog.csdn.net/weixin_42024288/article/details/80364441

https://blog.csdn.net/weixin_42024288/article/details/80364441 1.安装webpy模块: pip install web.py==0.40.dev 另外将: C:\Python34\Lib\site-packages\web.py-0.40.dev0-py3.4.egg\web\template.py 中约1022行return Template(open(path).read(), filename=path, **self._ke

Nginx+uwsgi+web.py配置

遇坑的同鞋可以留意一下 操作系统:Centos7 准备文件:Python-2.7.13.tgz下载地址:https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgznginx-1.12.0.tar.gz下载地址:http://nginx.org/download/nginx-1.12.0.tar.gzuwsgi-2.0.15.tar.gz下载地址:https://projects.unbit.it/downloads/uwsgi-2.0.1

lighttpd, web.py, spawning fcgi failed

lighttpd, web.py, spawning fcgi failed 基于web.py的程序开发起来还是非常简单的,但是我没想到在服务器上部署的时候却遇到了不少麻烦.我用的 web server 是 lighttpd,不能正常启动,查看错误日志,发现如下几行: 2009-12-15 19:48:04: (server.c.1503) server stopped by UID = 0 PID = 25128 2009-12-15 19:48:30: (log.c.166) server