简体   繁体   English

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

[英]How can I upload static files in GAE(python) with app.yaml?

I'm making a project using GAE, and have a terrible problem. 我正在使用GAE制作一个项目,并且遇到了一个可怕的问题。

I wanted to make a twitter bot, so I started the first step with posting tweets. 我想制作一个Twitter机器人,所以我开始发布推文的第一步。 I made the 'tweets.txt' in the same folder as the 'dailybasic.py'. 我在'dailybasic.py'所在的文件夹中创建了'tweets.txt'。

Here's some parts of the codes. 这是代码的一些部分。

#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 (it works, no error) 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 (run every 5 minutes) 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.yaml

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

I googled about this problem, and tried everything I can put in that '##' part of the 'app.yaml', but it didn't worked(it was able to be deployed, but GAE warned as 'File referenced by handler not found: dailybasic.py'). 我搜索了这个问题,并尝试了我可以放在'app.yaml'的'##'部分的所有内容,但它没有用(它可以部署,但是GAE警告为'由处理程序引用的文件'找不到:dailybasic.py')。

Here's a file tree: 这是一个文件树:

  • root
    • dailybasic dailybasic
      • dailybasic.py dailybasic.py
      • tweets.txt tweets.txt
    • main.py main.py
    • app.yaml, cron.yaml, index.yaml app.yaml,cron.yaml,index.yaml
    • index.html 的index.html

I want to keep the 'index.html' contains only html codes, without any scripts. 我想保持'index.html'只包含html代码,没有任何脚本。

How should I place the files and write the app.yaml? 我应该如何放置文件并编写app.yaml?

(And sorry for the poor English) (抱歉英语不好)

*added *添加

The problem is, open() doesn't works, because the 'tweets.txt' is not uploaded or in the wrong directory. 问题是,open()不起作用,因为'tweets.txt'没有上传或在错误的目录中。

Static files can only be served directly to the user at the URL specified in app.yaml. 静态文件只能在app.yaml中指定的URL处直接提供给用户。 They cannot be read by your application, as they are deployed to servers that only serve static files, and not to the infrastructure that runs your application. 它们无法被您的应用程序读取,因为它们被部署到仅提供静态文件的服务器,而不是部署到运行应用程序的基础结构。

If you only need to read the files from your script, just upload them as non-static. 如果您只需要从脚本中读取文件,只需将它们作为非静态文件上传即可。 If you need to both serve the files statically directly to the user's browser and read them from your scripts, you'll need to include 2 copies of the files in your application (although a symlink in a non-static directory will count as a second copy and get deployed). 如果您需要将文件静态地直接提供给用户的浏览器并从脚本中读取它们,则需要在应用程序中包含2个文件副本(尽管非静态目录中的符号链接将计为第二个复制并部署)。

Paths are specified relative to the directory containing app.yaml, so try this: 路径是相对于包含app.yaml的目录指定的,所以试试这个:

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

Did you want to map the file index.html to the root url / ? 您是否要将文件index.html映射到根URL / App engine doesn't do this automatically like some other web servers. 应用引擎不像其他一些Web服务器那样自动执行此操作。 To do this mapping, try something like this: 要执行此映射,请尝试以下操作:

- url: /
  static_files: index.html
  upload: index.html

why don't upload the file in the main directory and just use: 为什么不上传主目录中的文件,只需使用:

open("tweets.txt") 

without a path. 没有路径。

I'm using it to read .csv files without problem in GAE. 我用GA来读取.csv文件而没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM