[英]Q: google app engine app.yaml how to handle urls from within main.py?
[英]Does main.py or app.yaml determine the URL used by the App Engine cron task in this example?
在此示例代碼中,應用程序的URL似乎由應用程序中的此行確定:
application = webapp.WSGIApplication([('/mailjob', MailJob)], debug=True)
而且在app.yaml的app處理程序中的這一行:
- url: /.*
script: main.py
但是,cron任務的URL由以下行設置:
url: /tasks/summary
因此,似乎cron實用程序將調用“ /tasks/summary
”,並且由於app處理程序,這將導致調用main.py
這是否意味着,就cron而言,應用程序中設置URL的行是無關緊要的:
application = webapp.WSGIApplication([('/mailjob', MailJob)], debug=True)
。 。 。 因為cron任務所需的唯一URL是app.yaml中定義的URL。
app.yaml
application: yourappname
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
script: main.py
cron.yaml
cron:
- description: daily mailing job
url: /tasks/summary
schedule: every 24 hours
main.py
#!/usr/bin/env python
import cgi
from google.appengine.ext import webapp
from google.appengine.api import mail
from google.appengine.api import urlfetch
class MailJob(webapp.RequestHandler):
def get(self):
# Call your website using URL Fetch service ...
url = "http://www.yoursite.com/page_or_service"
result = urlfetch.fetch(url)
if result.status_code == 200:
doSomethingWithResult(result.content)
# Send emails using Mail service ...
mail.send_mail(sender="admin@gmail.com",
to="someone@gmail.com",
subject="Your account on YourSite.com has expired",
body="Bla bla bla ...")
return
application = webapp.WSGIApplication([
('/mailjob', MailJob)], debug=True)
def main():
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()
你可以這樣做:
app.yaml
application: yourappname
version: 1
runtime: python
api_version: 1
handlers:
- url: /tasks/.*
script: main.py
cron.yaml
cron:
- description: daily mailing job
url: /tasks/summary
schedule: every 24 hours
main.py
#!/usr/bin/env python
import cgi
from google.appengine.ext import webapp
from google.appengine.api import mail
from google.appengine.api import urlfetch
class MailJob(webapp.RequestHandler):
def get(self):
# Call your website using URL Fetch service ...
url = "http://www.yoursite.com/page_or_service"
result = urlfetch.fetch(url)
if result.status_code == 200:
doSomethingWithResult(result.content)
# Send emails using Mail service ...
mail.send_mail(sender="admin@gmail.com",
to="someone@gmail.com",
subject="Your account on YourSite.com has expired",
body="Bla bla bla ...")
return
application = webapp.WSGIApplication([
('/tasks/summary', MailJob)], debug=True)
def main():
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()
看起來你正在閱讀這個頁面 (即使你沒有給我們提供URL)。 所提供的配置和代碼將無法成功運行:cron任務將嘗試訪問URL路徑/任務/摘要,app.yaml將使其執行main.py,但后者僅為/ mailjob設置處理程序,因此cron任務的嘗試將失敗,並帶有404狀態代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.