python模拟开发一个网站

来源:互联网 发布:诊疗指南软件下载 编辑:程序博客网 时间:2024/05/18 16:58
# _*_ coding:utf-8 _*_import weburls = (    '/','Index',    '/reg','Reg',    '/user','User',    '/login','Login',)web.config.debug=False #设置成非调测模式app = web.application(urls,globals())session = web.session.Session(app,web.session.DiskStore('G:\\sessions')) #创建一个session对象render = web.template.render('templates')  #去搜索文件下的资源db = web.database(dbn = 'mysql',host='127.0.0.1',port='3306',user='root',pw='root',db='test',charset='utf8')class Index:    def GET(self):#方法名字根据用户请求方式定义        return render.index()    def POST(self):        i = web.input()        username = i.username        password = i.password        data=db.query("select * from user where username ='%s' and password = '%s' " %(username,password))        if not data:return '账号或者密码错误'        web.setcookie('user','%s' %username,600000)        web.setcookie('pwd','%s' %password,600000)        session.logged_id = True        raise web.sessother('/user')class Reg:    def GET(self):        return  render.reg()    def POST(self):        i = web.input() #用户发送的请求        username = i.username        password = i.password        password2 = i.password2        if password != password2:            return '两次密码不一致'        else:            #插入用户信息前做用户是否已经存在注册过,密码进行佳美            db.query("insert into user(userid,username,password,text) values(null,'%s','%s',%s)" %(username,password,0))            session.logged_in = True #当前用户的session生效后就有权限登录            raise web.seeother('/user')#cookies,sessionclass User:    def GET(self):        if session.get('logged_in'):            return render.user()        else:            return '还没有登录或过期'    def POST(self):        url = web.input().link        file,title=get_file.getfile(url)        if not session.get('logged_in'):return '还没有权限'        web.header('Content-Disposition:attachment;filename =%s.doc' %title)        web.header('Content-Type:application/msword;')        return fileif __name__ == '__main__':    app.run()
0 0