V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
DAOCLOUD
推荐学习书目
Python Cookbook
Using Google App Engine
推荐下载
Latest Google App Engine SDK
其他兼容技术
AppScale
Mianco
V2EX  ›  Google App Engine

这两段代码有什么区别?

  •  
  •   Mianco · 2010-12-04 23:41:13 +08:00 · 3748 次点击
    这是一个创建于 4884 天前的主题,其中的信息可能已经有所发展或是发生改变。
    print 'Content-Type: text/html; charset=UTF-8'
    print ''
    print '<html><meta http-equiv="refresh" content="0;url=/index.html"></html>'

    第二段代码

    import wsgiref.handlers
    import os
    from google.appengine.ext import webapp
    class MainPage(webapp.RequestHandler):
    def get(self):
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    f=open(path)
    self.response.out.write(f.read())
    f.close()
    def main():
    application = webapp.WSGIApplication([('/', MainPage)],debug=True)
    wsgiref.handlers.CGIHandler().run(application)
    if __name__ == "__main__":
    main()

    为什么一个起作用一个不起作用(本地测试),复杂的那个起作用了。
    4 条回复    1970-01-01 08:00:00 +08:00
    xinzhi
        1
    xinzhi  
       2010-12-04 23:50:08 +08:00
    一个是http头跳转,在浏览器端才有效;后一个是python读取文件直接输出。
    vayn
        2
    vayn  
       2010-12-04 23:52:08 +08:00
    print '' 换成 print
    est
        3
    est  
       2010-12-05 00:39:48 +08:00
    区别是前者是cgi模式,后者是google家的webapp框架。
    keakon
        4
    keakon  
       2010-12-05 02:22:52 +08:00
    楼主这个问题很蛋疼,撇开其技术实现,2段代码所完成的功能都截然不同。

    第一段是CGI输出HTML,然后用HTML的meta跳转来重定向到/index.html。
    懂HTTP的都知道,其实不需要这么麻烦,直接输出这个就行了:
    print 'Location: /index.html'

    第二段代码是用WSGI生成HTML,而且是直接读取index.html的内容来输出的,没有进行重定向。

    从楼主的描述来看,你应该是没有处理/index.html这个URL。
    我猜你应该是用一个Python script处理了所有请求,然后直接重定向到/index.html;但是/index.html也是被这个script处理的,于是再次重定向到/index.html;于是你就蛋疼地一直重定向了…

    技术上来说,你的要求完全不需要用Python来处理,直接改app.yaml用静态文件处理就行了:
    handlers:

    - url: /(index.html)?
    static_files: index.html
    upload: index.html
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5665 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 01:52 · PVG 09:52 · LAX 18:52 · JFK 21:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.