python - 如何使用 app.yaml 在 GAE(python) 中上传静态文件?

标签 python google-app-engine file-upload twitter yaml

我正在使用 GAE 做一个项目,遇到了一个可怕的问题。

我想制作一个 Twitter 机器人,所以我从发布推文开始了第一步。我在与“dailybasic.py”相同的文件夹中制作了“tweets.txt”。

这是部分代码。

#app.yaml

application: mathgirlna
version: 1
runtime: python
api_version: 1

handlers:
# - url: /static
#  static_dir: static

- url: /dailybasic   
  script: dailybasic/dailybasic.py 

- url: /.*
  script: main.py

main.py(有效,没有错误)

#!/usr/bin/python  
# -*- coding: utf-8 -*-

import os
import sys

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import wsgiref.handlers

class MainPage(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, None))


application = webapp.WSGIApplication([('/', MainPage)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

dailybasic.py(每 5 分钟运行一次)

#!/usr/bin/python  
# -*- coding: utf-8 -*-

import os
import sys
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
import tweepy
import wsgiref.handlers
import time

def tweetit(tweet):
   if len(tweet)<140:
      api.update_status(tweet)
   else:
      diaryentries.append(tweet)

consumer_key = '******************'
consumer_secret = '*******************************************'
access_token = '**************************************************'
access_token_secret = '****************************************'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

class dailybasic(webapp.RequestHandler):
    def get(self):
        now = time.localtime()
        path = os.path.join(os.path.dirname(__file__), 'tweets.txt')
        f_db = open(path, 'r')
        db = f_db.readline() 
        while db != '':
            todaynow = []
            wday = now.tm_wday
            if db[(wday+1)%7]=='1' and now.tm_hour * 60 + now.tm_min <= int(db[8:10]) * 60 + int(db[11:13]) and now.tm_hour * 60 + now.tm_min + 5 > int(db[8:10]) * 60 + int(db[11:13]) :
                todaynow.append(db[14:])
        if(len(todaynow) != 0):
            import random
            tweetit(todaynow[random.randrange(0,len(todaynow)-1)])


application = webapp.WSGIApplication([('/dailybasic', dailybasic)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

cron.yaml

cron:
- description: day process
  url: /dailybasic
  schedule: every 5 minutes from 06:00 to 01:30
  timezone: Asia/Seoul

我用谷歌搜索了这个问题,并尝试了所有我可以放入“app.yaml”的“##”部分的所有内容,但它没有用(它能够部署,但 GAE 警告为"file"未找到处理程序引用:dailybasic.py')。

这是一个文件树:

    • 日常基础
      • dailybasic.py
      • 推文.txt
    • 主要文件
    • app.yaml、cron.yaml、index.yaml
    • index.html

我想保持“index.html”只包含 html 代码,不包含任何脚本。

app.yaml应该怎么放?

(抱歉英语不好)

*添加

问题是,open() 不起作用,因为“tweets.txt”未上传或位于错误的目录中。

最佳答案

静态文件只能通过 app.yaml 中指定的 URL 直接提供给用户。它们无法被您的应用程序读取,因为它们被部署到仅提供静态文件的服务器,而不是运行您的应用程序的基础设施。

如果您只需要从脚本中读取文件,只需将它们作为非静态文件上传即可。如果您需要将文件直接静态地提供给用户的浏览器并从您的脚本中读取它们,您需要在您的应用程序中包含文件的 2 个副本(尽管非静态目录中的符号链接(symbolic link)将算作第二个)复制并部署)。

关于python - 如何使用 app.yaml 在 GAE(python) 中上传静态文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7307878/

相关文章:

Python argparse : Processing Certain Arguments Before Adding Others

python - 在某些约束下生成随机数

google-app-engine - 使用 App Engine 开发服务器向 Google API 声明身份

google-app-engine - 访问 golang 模板中引用对象的属性(Google 应用引擎)

c# - NeatUpload 在远程服务器上上传 pdf 和 gif 文件时出现问题

python - 如何使用上下文管理器来避免在 python 中使用 __del__?

python - 爬行完成后运行自定义代码(scrapy)

java - 如何使用GAE将文件从android应用上传到谷歌应用引擎

javascript - AWS SDK JavaScript : how display upload progress of AWS. S3.putObject?

php - Javascript大文件 uploader