Pappon webapp dynamic paths

I'm currently experimenting with some APIs, and I will be interested to know how to use the URLs as a parameter for the application. For instance:

http://www.myapp.com/myapp/jack prints "hello jack"

or

http://www.myapp.com/myapp/john prints "hello john"

or http://www.myapp.com/myapp/john%20jack prints "hello john jack"

I would like to point out some pointers to where I can find this functionality. I feel like it's easy, but I just don't get it. Does it depend on the structure used? I am very new to Python, so I still agree with Django and the like. I use Python in Google App Engine with integrated webapp platform with GAE.

This is currently the code I'm working with right now:

import cgi from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app class MainPage(webapp.RequestHandler): def get(self,url=None): self.response.out.write("hello " + str(url)) application = webapp.WSGIApplication([ (r'/(\w+)', MainPage) ], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main() 
+4
source share
2 answers

Wei

You need to create a url template, for example, when you receive a request in myapp , you will parse the remaining URL and display a message.

eg

File helloworld/app.yaml

 application: helloworld version: 1 runtime: python api_version: 1 handlers: - url: /.* script: helloworld.py 

File helloworld/helloworld.py

 from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app class MainPage(webapp.RequestHandler): def get(self, url=None): self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('Happy New Year '+str(url)) #application = webapp.WSGIApplication( # [('/', MainPage)], # debug=True) application = webapp.WSGIApplication([ (r'/myapp/(?P<url>\d{4})/$', MainPage) ], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main() 

This way it will handle everything you request in /myapp/year/ , so from this you should get the value after /myapp/ and display the year.

Note. Make your long url yours so you understand how it will work :).

+3
source

I'm just wondering if a tag ?P<url> needed?

I managed to solve the problem simply using

 application = webapp.WSGIApplication([ (r'/myapp(/.*)*?', MainPage) ], debug=True) 

Thanks to Lafada and Wei Hao for sharing with us. I am also stuck in this problem and I found this discussion very useful :)

0
source

Source: https://habr.com/ru/post/1397965/


All Articles