2011-09-05 52 views
3

我在做一個使用GAE的項目,並且有一個可怕的問題。如何使用app.yaml將GAE(python)中的靜態文件上傳?

我想製作一個twitter bot,所以我開始發佈推文的第一步。我在與'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警告'處理程序未引用文件:dailybasic.py')。

這裏有一個文件樹:

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

我想要保持 '的index.html' 只包含HTML代碼,沒有任何腳本。

我該如何放置文件並寫入app.yaml?

(而對於英文不好對不起)

*增加

的問題是,開放式()不工作,因爲「tweets.txt」沒有上傳或在錯誤的目錄。

回答

1

路徑相對於包含app.yaml的目錄中指定,那麼試試這個:

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

你想對文件映射的index.html到根網址/? App引擎不會像其他一些Web服務器那樣自動執行此操作。要做到這一點映射,嘗試這樣的事情:

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

嗯,我仍然有同樣的問題,但寫它們會很有用 - 我想。謝謝。 –

+0

好的。我必須清楚 - 我想上傳'tweets.txt',並讓python腳本讀取它。 –

+0

@Sidus_smg:當你做什麼時,你會得到什麼錯誤信息? –

7

靜態文件只能在app.yaml中指定的URL直接提供給用戶。它們不能被應用程序讀取,因爲它們被部署到僅提供靜態文件的服務器,而不是運行應用程序的基礎架構。

如果您只需要從腳本中讀取文件,只需將其上傳爲非靜態文件。如果您需要將文件靜態直接提供給用戶的瀏覽器並從腳本中讀取它們,則需要在應用程序中包含2個文件副本(儘管非靜態目錄中的符號鏈接將作爲第二個複製並部署)。

0

爲什麼不上傳文件中的主目錄下,只需使用:

open("tweets.txt") 

沒有路徑。

我正在使用它讀取GAE中沒有問題的.csv文件。

相關問題