google(一)python使用入门

google(一)python使用入门

我在google申请了帐号,呵呵,想试试在那里发布我新写的简单python应用。
首先学习google的python入门文档:
http://code.google.com/intl/zh-CN/appengine/docs/python/gettingstarted/
下载google的sdk
http://googleappengine.googlecode.com/files/google_appengine_1.3.0.zip
解开压缩文件,打开里面的readme,参考该文档安装环境,我的操作系统是ubuntu9.10
解开压缩后,将文件夹拷贝到工作目录
mv google_appengine/ /home/sillycat/tools/

1.撰写简单的hello world
创建目录
mkdir easyappenginepython
按照入门文档,我新建了文件helloworld.py,然后键入了如下内容:
print "Content-Type: text/plain"
print ""
print "Hello,sillycat!"
定义了google app engine需要的配置文件app.yaml,内容如下:
application: easyappenginepython
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
script: helloworld.py
切换到命令工具目录
cd /home/sillycat/tools
执行了命令:
google_appengine/dev_appserver.py /home/sillycat/work/easyappenginepython/
然后访问地址http://localhost:8080就看到了我的测试页面了。

2.使用webapp框架
按照文档上的描述,webapp包含三部分:
一个或多个 RequestHandler 类,用于处理请求和构建响应
一个 WSGIApplication 实例,按照网址将收到的请求发送给处理程序
一个主要例行程序,用于使用 CGI 适配器运行 WSGIApplication

新建package helloworld
新建文件helloworld/helloworld.py,内容如下:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, sillycat webapp World!')

application = webapp.WSGIApplication(
[('/', MainPage)],
debug=True)

def main():
run_wsgi_app(application)

if __name__ == "__main__":
main()
修改app.yaml配置文件:
application: easyappenginepython
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
script: helloworld/helloworld.py
启动服务:
切换到命令工具目录
cd /home/sillycat/tools
执行了命令:
google_appengine/dev_appserver.py /home/sillycat/work/easyappenginepython/
然后访问地址http://localhost:8080就看到了新页面了

webapp的详细描述
http://code.google.com/intl/zh-CN/appengine/docs/python/tools/webapp/

3.用户服务
参考文档,修改了helloworld.py文件如下:
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
def get(self):
user = users.get_current_user()

if user:
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, you are ' + user.nickname())
else:
self.redirect(users.create_login_url(self.request.uri))

application = webapp.WSGIApplication(
[('/', MainPage)],
debug=True)

def main():
run_wsgi_app(application)

if __name__ == "__main__":
main()
详细介绍用户API的文档:
http://code.google.com/intl/zh-CN/appengine/docs/python/users/

4.处理表单
简单的按照文档上,修改了helloworld.py如下:
import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
def get(self):
self.response.out.write("""
<html>
<body>
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</body>
</html>""")


class Guestbook(webapp.RequestHandler):
def post(self):
self.response.out.write('<html><body>You wrote:<pre>')
self.response.out.write(cgi.escape(self.request.get('content')))
self.response.out.write('</pre></body></html>')

application = webapp.WSGIApplication(
[('/', MainPage),
('/sign', Guestbook)],
debug=True)

def main():
run_wsgi_app(application)

if __name__ == "__main__":
main()
python运行时环境详细文档:
http://code.google.com/intl/zh-CN/appengine/docs/python/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值