Sample Code from Developing and deploying an application on Google App Engine

Read Time:1 Minute, 11 Second

In the video, it provides the code as below

#!/usr/bin/env python

import os
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

class MyHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write(template.render('main.html', {}))

def main():
    app = webapp.WSGIApplication([
            (r'.*', MyHandler)], debug= True)
    wsgiref.handlers.CGIHandler().run(app);

if __name__ == "__main__":
    main()

However, it gives the error as below:

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
    self.finish_response()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
    self.write(data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 202, in write
    assert type(data) is StringType,"write() argument must be string"
AssertionError: write() argument must be string

To fix this error, I have found this article.

According to answer, it is unicode problem.

So fixed version of code is below:

#!/usr/bin/env python

import os
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

class MyHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write(unicode(template.render('main.html', {})))

def main():
    app = webapp.WSGIApplication([
            (r'.*', MyHandler)], debug= True)
    wsgiref.handlers.CGIHandler().run(app);

if __name__ == "__main__":
    main()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.