如何使用app.yaml在GAE(python)中上传静态文件?
我正在使用 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
我在 google 上搜索了这个问题,并尝试了我可以放入“app.yaml”的“##”部分中的所有内容,但是它不起作用(它能够部署,但 GAE 警告为“未找到处理程序引用的文件:dailybasic.py”)。
这是一个文件树:
- root
- 每日基本
- dailybasic.py
- tweets.txt
- main.py
- app.yaml、cron.yaml、index.yaml
- index.html
- 每日基本
我想保留'index.html'仅包含html代码,没有任何脚本。
我应该如何放置文件并编写app.yaml?
(对英语不好感到抱歉)
*补充
问题是,open() 不起作用,因为“tweets.txt”未上传或位于错误的目录中。
I'm making a project using GAE, and have a terrible problem.
I wanted to make a twitter bot, so I started the first step with posting tweets. I made the 'tweets.txt' in the same folder as the 'dailybasic.py'.
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)
#!/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)
#!/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
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').
Here's a file tree:
- root
- dailybasic
- dailybasic.py
- tweets.txt
- main.py
- app.yaml, cron.yaml, index.yaml
- index.html
- dailybasic
I want to keep the 'index.html' contains only html codes, without any scripts.
How should I place the files and write the 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
静态文件只能通过 app.yaml 中指定的 URL 直接提供给用户。它们无法被您的应用程序读取,因为它们部署到仅提供静态文件的服务器,而不是部署到运行您的应用程序的基础设施。
如果您只需要从脚本中读取文件,只需将它们作为非静态上传即可。如果您需要将文件直接静态地提供给用户的浏览器并从脚本中读取它们,则需要在应用程序中包含文件的 2 个副本(尽管非静态目录中的符号链接将计为第二个)复制并部署)。
Static files can only be served directly to the user at the URL specified in app.yaml. 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).
路径是相对于包含 app.yaml 的目录指定的,因此请尝试以下操作:
是否要将文件 index.html 映射到根 url
/
? App Engine 不会像其他一些 Web 服务器那样自动执行此操作。要执行此映射,请尝试以下操作:Paths are specified relative to the directory containing app.yaml, so try this:
Did you want to map the file index.html to the root url
/
? App engine doesn't do this automatically like some other web servers. To do this mapping, try something like this:为什么不在主目录中上传文件并仅使用:
不带路径。
我用它在 GAE 中毫无问题地读取 .csv 文件。
why don't upload the file in the main directory and just use:
without a path.
I'm using it to read .csv files without problem in GAE.