V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
DAOCLOUD
推荐学习书目
Python Cookbook
Using Google App Engine
推荐下载
Latest Google App Engine SDK
其他兼容技术
AppScale
Mianco

这两段代码有什么区别?

  •  
  •   Mianco · Dec 4, 2010 · 4601 views
    This topic created in 5635 days ago, the information mentioned may be changed or developed.
    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 replies    1970-01-01 08:00:00 +08:00
    xinzhi
        1
    xinzhi  
       Dec 4, 2010
    一个是http头跳转,在浏览器端才有效;后一个是python读取文件直接输出。
    vayn
        2
    vayn  
       Dec 4, 2010
    print '' 换成 print
    est
        3
    est  
       Dec 5, 2010
    区别是前者是cgi模式,后者是google家的webapp框架。
    keakon
        4
    keakon  
       Dec 5, 2010
    楼主这个问题很蛋疼,撇开其技术实现,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
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2783 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 01:40 · PVG 09:40 · LAX 18:40 · JFK 21:40
    ♥ Do have faith in what you're doing.