开发者

generating non-html output from google's app engine

I'm trying to dynamically generate iCal output using Max M's icalendar python module and google's app engine. However, when I omit the boiler plate <html> header and footer tags on the output from webapp.RequestHandler, it looks like the HTTP 200 response is added to the top of the file.

When I register this code:

class Calendar(webapp.RequestHandler):
    def get(self):
        self.response.out.write('BEGIN:VCALENDAR\n')
        [...]

with webapp.WSGIApplication, the response looks like:

Status: 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Length: 11133

BEGIN:VCALENDAR
[...]

How do I omit开发者_运维技巧 the <html> tags and not get the HTTP 200 response splatted at the top of the page?


Webapp and App Engine don't care what content type you return, so the issue is unrelated to the tag (or lack thereof). You likely have a print statement somewhere in your code, which is causing the headers to be sent as part of the body of the response. You should never use print in a WSGI app - always use self.response.out.write as in the snippet you pasted.


You can't, the "Status: 200 Ok....." is the HTTP header which will be outputted on every request by the web server. Any web server will do the same thing.

Your client will need to read the body of the http response.


Not sure if this is the real problem or not, but you should be setting the content-type to indicate that this is calendar data, not HTML:

Content-Type: text/html; charset=utf-8

should be

Content-Type: text/calendar;


This seems to be outputting standard HTTP response headers. Try adding "Content-Disposition: attachment; filename='%s'" % filename HTTP header to your response to get desired file downloaded.


Something else to consider:

If you are importing code from another file, and that other file is a webapp, you need to make sure that it only runs as a webapp if it is the main

Anotherwords - Go from this

application = webapp.WSGIApplication(
                                 [
                                  ('/locate', Request),
                                   ],
                                 debug=False)

util.run_wsgi_app(application)

to this

application = webapp.WSGIApplication(
                                 [
                                  ('/locate', Request),
                                   ],
                                 debug=False)

def main():
   util.run_wsgi_app(application)

if __name__ == "__main__":
   main()

This problem was killing me for hours, and this was one of the only posts I could find that was close to it. I'm hoping this will help anyone else with the same problem as me!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜