开发者

Appengine GET parameters

I am not really familiar with Python and am trying to transform one of my php webapps to python. Currently I am running the app on localhost using the appengine launcher and this is what I am trying to do.

I am trying to get a list of all the parameters posted to the url and then submit them to a page and get its conten开发者_运维技巧t.

So basically: 1: get the params 2: get contents of a url by submitting those params (the PHP equivalent of curl of file_get_contents)

This is my code so far

from google.appengine.ext import webapp

class MyHandler(webapp.RequestHandler):
    def get(self):
        name1 = self.request.get_all("q")
        name2 = self.request.get_all("input")
        return name1,name2

x = MyHandler()
print x.get() 

and the url

http://localhost:8080/?q=test1&input=test2

and this is the error I get

AttributeError: 'MyHandler' object has no attribute 'request'

Now I cant get it to print anything and I am not sure how I can get the contents of another url by submitting name1 and name2

I have tried looking at the documentation but I cant make sense of it since all they have is just 2 lines of code to get the use of function started.


x = MyHandler()
print x.get() 

This is not a typical part of an AppEngine app. You don't use print to return output to the browser.

When you create a new app in AppEngineLauncher it gives you a skeleton project that looks like this:

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util


class MainHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('Hello world!')


def main():
    application = webapp.WSGIApplication([('/', MainHandler)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()

Your app has to be run similarly. You need a main() method that creates a wsgi_app which is in charge of calling your handler. That main() function is called by dev_appserver, assuming your app.yaml file is set up correctly.

def get(self):
    name1 = self.request.get_all("q")
    name2 = self.request.get_all("input")
    self.response.out.write(name1 + ',' + name2)

Should work if you've set up your app correctly.


You will need a few more lines of code to make this work if you are going to use the WebApp framework. Stick the following lines at the end of your code (and get rid of the last two lines where you instantiate your class and call the get method)

application = webapp.WSGIApplication([('/', MyHandler)])

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜