Dreen
Dreen

Reputation: 7226

cgi.test() in Google App Engine

Im currently learning to use Google App Engine, and I modified this example to look like this:

import cgi
import webapp2

from google.appengine.api import users

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("""
          <html>
            <body>
              <form action="/sign" method="post">
                <div><textarea name="content" rows="3" cols="60"></textarea></div>
                <div><input type="submit" value="Sign Guestbook"></div>
              </form>
            </body>
          </html>""")


class Guestbook(webapp2.RequestHandler):
    def post(self):
        cgi.test()

app = webapp2.WSGIApplication([('/', MainPage),
                              ('/sign', Guestbook)],
                              debug=True)

Because I wanted to see what does cgi.test() do. It produces output that matches the description in Python documentation, but it incorrectly says there is no POST data. Additionally, it informs about the following error:

  File "C:\Python27\lib\cgi.py", line 918, in test
    print_environ(environ)
  File "C:\Python27\lib\cgi.py", line 944, in print_environ
    print "<DT>", escape(key), "<DD>", escape(environ[key])
  File "C:\Python27\lib\cgi.py", line 1035, in escape
    s = s.replace("&", "&amp;") # Must be done first!
AttributeError: 'LogsBuffer' object has no attribute 'replace'

This is on a localhost development environment. Why am I getting incorrect results? The example notes that not all Python functions are allowed though I doubt this would be the case with cgi.test(), would it?

edit: Do I have to change app.yaml in some way to allow for special treatment of http://localhost:8080/sign?

Upvotes: 1

Views: 606

Answers (1)

thikonom
thikonom

Reputation: 4267

The problem is that the value of the wsgi.errors(and wsgi.input) is the actual instance: For example it it some like this:

'wsgi.errors': <google.appengine.api.logservice.logservice.LogsBuffer object at 0x105219150>

and not its string representation and the escape method can only be called on a string.

(Dirty hack) Find the file google/appengine/runtime/request_environment.py (i don't provide the full path since i don't know where your installation is located) and then at lines 111-112:

replace:

def __getitem__(self, key):
    return self._request.environ[key]

with:

def __getitem__(self, key):
    if key in ['wsgi.errors', 'wsgi.input']:
        return str(self._request.environ[key])
    return self._request.environ[key]

Upvotes: 1

Related Questions