第一个App engine程序
2008-06-16 09:40 Jaypei 阅读(445) 评论(0) 编辑 收藏 举报
新建app.yaml:
main.py文件:
模板文件main.html:
然后可使用dev_appserver.py进行本地测试:
使用appcfg.py上传到服务器:
可以使用http://jaypei.appspot.com进行访问了。
application: jaypei
version: 1
api_version: 1
runtime: python
handlers:
- url: .*
script: main.py
version: 1
api_version: 1
runtime: python
handlers:
- url: .*
script: main.py
main.py文件:
#!/usr/bin/evn python
import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
class Shout(db.Model):
message = db.StringProperty(
required=True)
who = db.StringProperty(
required=False)
when = db.DateTimeProperty(
auto_now_add=True)
class MyHandler(webapp.RequestHandler):
def get(self):
shouts = db.GqlQuery("SELECT * FROM Shout "
"ORDER BY when DESC")
values = {"shouts" : shouts}
values["var1"] = "Jaypei"
self.response.out.write(template.render("main.html", values))
def post(self):
shout = Shout(
message = self.request.get('message'),
who = self.request.get('who'))
shout.put()
self.redirect('/')
def main():
app = webapp.WSGIApplication([
(r".*", MyHandler)], debug=True)
wsgiref.handlers.CGIHandler().run(app)
if __name__=="__main__":
main()
import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
class Shout(db.Model):
message = db.StringProperty(
required=True)
who = db.StringProperty(
required=False)
when = db.DateTimeProperty(
auto_now_add=True)
class MyHandler(webapp.RequestHandler):
def get(self):
shouts = db.GqlQuery("SELECT * FROM Shout "
"ORDER BY when DESC")
values = {"shouts" : shouts}
values["var1"] = "Jaypei"
self.response.out.write(template.render("main.html", values))
def post(self):
shout = Shout(
message = self.request.get('message'),
who = self.request.get('who'))
shout.put()
self.redirect('/')
def main():
app = webapp.WSGIApplication([
(r".*", MyHandler)], debug=True)
wsgiref.handlers.CGIHandler().run(app)
if __name__=="__main__":
main()
模板文件main.html:
<h1>Hello {{var1}}.</h1>
{% for shout in shouts %}
<div>
{{shout.message}}
from
{% ifequal shout.who None %}
Anonymous
{% else %}
{{shout.who}}
{% endifequal %}
</div>
{% endfor %}
<form action="" method="post" accept-charset="utf-8">
<p>Message: <input type="text" name="message" id="message" /></p>
<p>From: <input type="text" name="who" id="who" /></p>
<p><input type="submit" value="shout!" /></p>
</form>
{% for shout in shouts %}
<div>
{{shout.message}}
from
{% ifequal shout.who None %}
Anonymous
{% else %}
{{shout.who}}
{% endifequal %}
</div>
{% endfor %}
<form action="" method="post" accept-charset="utf-8">
<p>Message: <input type="text" name="message" id="message" /></p>
<p>From: <input type="text" name="who" id="who" /></p>
<p><input type="submit" value="shout!" /></p>
</form>
然后可使用dev_appserver.py进行本地测试:
dev_appserver.py --port 2000 jaypei\
INFO 2008-06-16 01:29:44,687 appcfg.py] Server: appengine.google.com
INFO 2008-06-16 01:29:44,697 appcfg.py] Checking for updates to the SDK.
INFO 2008-06-16 01:29:45,555 appcfg.py] The SDK is up to date.
WARNING 2008-06-16 01:29:45,878 dev_appserver.py] Could not initialize images A
PI; you are likely missing the Python "PIL" module. ImportError: No module named
PIL
INFO 2008-06-16 01:29:45,905 dev_appserver_main.py] Running application jayp
ei on port 2000: http://localhost:2000
INFO 2008-06-16 01:29:44,697 appcfg.py] Checking for updates to the SDK.
INFO 2008-06-16 01:29:45,555 appcfg.py] The SDK is up to date.
WARNING 2008-06-16 01:29:45,878 dev_appserver.py] Could not initialize images A
PI; you are likely missing the Python "PIL" module. ImportError: No module named
PIL
INFO 2008-06-16 01:29:45,905 dev_appserver_main.py] Running application jayp
ei on port 2000: http://localhost:2000
使用appcfg.py上传到服务器:
appcfg.py update jaypei\
Loaded authentication cookies from C:\Users\Jaypei/.appcfg_cookies
Scanning files on local disk.
Initiating update.
Email: ***@gmail.com
Password for ***@gmail.com:
Saving authentication cookies to X:\Users\***/.appcfg_cookies
Cloning 3 application files.
Closing update.
Uploading index definitions.
Scanning files on local disk.
Initiating update.
Email: ***@gmail.com
Password for ***@gmail.com:
Saving authentication cookies to X:\Users\***/.appcfg_cookies
Cloning 3 application files.
Closing update.
Uploading index definitions.
可以使用http://jaypei.appspot.com进行访问了。