How can I upload static files in GAE(python) with app.yaml?
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 go开发者_如何学编程ogle.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.
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).
Paths are specified relative to the directory containing app.yaml, so try this:
handlers:
- url: /dailybasic
script: dailybasic/dailybasic.py
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:
- 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.
精彩评论