zoukankan      html  css  js  c++  java
  • 部署新浪SAE web.py Session及图片上传等问题注意事项

    1.以下几条代码解决编码问题

    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')

    2.图片上传问题

    需要开通sina的Storage服务,随便建个什么Domain,注意HTML代码,那个enctype属性一定要写,两个上传图片的控件叫avatar和photo

    <form action="details" method="post" enctype="multipart/form-data">

    以下web.input中要给出参数,不然无法读取图片和图片名

    i=web.input(avatar={},photo={})
    var = dict(name=shop_session.username)
    n = db.update('T_User', where='username = $name', vars=var, person_name=i.name, sex=i.sex,birthday=i.birthday,address=i.address,hobby=i.hobby,avatar='http://dataguruzw-attachment.stor.sinaapp.com/'+shop_session.username+"_avatar.jpg",photo='http://dataguruzw-attachment.stor.sinaapp.com/'+shop_session.username+"_photo.jpg",introduce=i.introduce)
    s=sae.storage.Client()
    ob = sae.storage.Object(i.avatar.file.read())
    s.put('attachment',shop_session.username+"_avatar.jpg",ob)
    ob = sae.storage.Object(i.photo.file.read())
    s.put('attachment',shop_session.username+"_photo.jpg",ob)

    3.Session问题

    网上转来的好文,感谢作者让我这个python小白顺利能完成作业,补充几点小白需要知道的python基础知识

    需要开通SAE的Storage,并添加名为Session的Domain,并引入以下几个是叫包还是什么,完后你的Session不叫Session,叫shop_session

    import time
    import sae.storage
    from sae.ext.storage import monkey

    1,解决官方sae样例代码中的误区 

    #app = web.application(urls, globals()).wsgifunc()
    app = web.application(urls, globals())

    #application = sae.create_wsgi_app(app)
    application = sae.create_wsgi_app(app.wsgifunc()) 

    注释掉代码为官方提供的样例代码,但官方代码为了方便在APP实例化时直接调用了wsgifunc(),但这样会造成部分webpy属性的调用错误.如调用app.add_processor的失败.

    2,设定全局session的调用 

    #增加session管理,将session放入全局的web.config
    if web.config.get('_session') is None:
    shop_session = web.session.Session(app, DiskStore(session_root),
    initializer={'login_shop_id':'NULL'})
    web.config._session = shop_session
    else:
    shop_session = web.config._session

    def session_hook():
    web.ctx.session = shop_session

    app.add_processor(web.loadhook(session_hook))


    3,解决sae环境中无法读写本地目录问题 

    SAE环境下python无法对本地目录进行读写操作,但sae提供了Storage,并且可以将Storage像本地磁盘一样挂载使用.所以添加如下代码: 

    monkey.patch_all()
    session_root = '/s/session/'

      

    4,解决SEA的Storage作为磁盘挂载后但不支持os.remove方法

    "目前支持(patch)的文件系统接口函数为: open, os.listdir, os.mkdir, os.path.exists, os.path.isdir, os.open, os.fdopen, os.close, os.chmod, os.stat, os.unlink, os.rmdir"但webpy在DiskStore.cleanup方法却是调用的os.remove,需要改为os.unlink

    #因新浪SAE的stroge不支持挂载为路径后的os.remove函数,故重新改在一下
    class DiskStore(web.session.DiskStore):
    def __init__(self, root):
    web.session.DiskStore.__init__(self, root)
    # if the storage root doesn't exists, create it.
    # self.root = root

    def __delitem__(self, key):
    path = self._get_path(key)
    if os.path.exists(path):
    os.unlink(path)

    def cleanup(self, timeout):
    now = time.time()
    for f in os.listdir(self.root):
    path = self._get_path(f)
    atime = os.stat(path).st_atime
    if now - atime > timeout :
    os.unlink(path)#改写为unlink函数

    OK,现在session已经可以正常使用了.

    editer by zdl0812@163.com 欢迎转载,请注明出处

  • 相关阅读:
    [Practical Git] Clean up commits with git rebase
    [Practical Git] Show who changed a line last with git blame
    [Practical Git] Compare file changes with git diff
    [Practical Git] Filter commit history with git log arguments
    [Practical Git] Format commit history with git log arguments
    [Practical Git] Navigate git command pager output with Unix less commands
    [Practical Git] Switching between current branch and last checkout branch
    [ES6] ES6 Parameter Object Destructuring with Required Values
    [React] Higher Order Components (replaces Mixins)
    HDU 1242 Rescue (BFS(广度优先搜索))
  • 原文地址:https://www.cnblogs.com/MarsMercury/p/4963046.html
Copyright © 2011-2022 走看看