在GAE(Google App Engine)上搭建python2.7的web.py程序

资源:
1、下载GAE的sdk for python。https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_Python
2、下载GAE支持的python版本。python2.5和python2.7
3、下载python的flup模块。【前提保证安装了setuptools,否则不能安装python模块】
4、安装MySQLdb模块。【可选,如果要用到mysql数据库的话】

体验hello world:
1、新建一个helloworld目录,注意是小写
2、其下新建一个helloworld.py文件
3、添加如下内容:
print 'Content-Type: text/plain'
print ''
print 'Hello, world! I\'m in GAE'
4、再新建一个app.yaml文件
5、添加内容如下:
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: no
handlers:
- url: /.*
  script: helloworld.py
6、启动gae的sdk所带的网络服务,在helloworld目录的父目录中运行如下命令:
dev_appserver.py helloworld/          #注意安装GAE时要选择添加安装目录到PATH中,否则此处命名无效
7、启动后没有错误的话,会提示你去访问localhost:8080,管理网址为:http://localhost:8080/_ah/admin/datastore

搭建web.py程序:
其实GAE自己也带有一个web程序,webapp,不过web.py的时候比较多。搭建web.py的步骤有如下几步:
1、复制Lib\site-packages\web安装目录到helloworld目录下
2、修改helloworl.py的内容如下:
	#encoding: utf-8
	import web
			
	urls = (
		'/(.*)', 'hello'
	)
	app = web.application(urls, globals())

	class hello:        
		def GET(self, name):
			if not name: 
				name = 'World'
			return 'Hello, ' + name + '!'

	if __name__ == "__main__":
		app.cgirun()   #注意这里不是app.run()
3、重新启动GAE的服务
4、访问http://localhost:8080

使用web.py的模板:
搭建上面内容都没有什么大错,但是使用web.py时肯定是要用到模板的,使用模板包含如下的步骤:
1、在helloworld目录下新建一个templates目录
2、在其下添加一个index.html文件
3、其内容如下:
$def with(name)
	<div><h1>Hello $name</h1></div>
4、修改helloworl.py文件如下:
#encoding: utf-8
import web

render = web.template.render('templates/', cache=False)

urls = (
	'/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:        
	def GET(self, name):
		if not name: 
			name = 'World'
		return render.index(name)

if __name__ == "__main__":
	app.cgirun()   
5、编译模板文件,原因网上都有。在helloworld主目录下运行如下命令:
python web/template.py --compile templates
6、编译成功后会创建一个__init__.py文件,并且每次模板有改动后需要重新编译
7、重新启动GAE,一切安好便是晴天,呵呵!

上传你应用:
至此,我们的demo文件已经完成了,接下来就是要上传至GAE的真实环境了。上传过程包括如下的步骤:
1、注册一个google帐号并申请一个Application ID
2、修改app.yaml文件中的application内容为你刚申请的Application ID
3、helloworld目录外使用命令行上传你的程序:appcfg.py update helloworld/  如果有ssl认证错误使用这个命令:appcfg.py update    --insecure helloworld/ 
4、输入申请Application ID的帐号和密码
5、上传成功你就大功告成啦!【 https://justfreelikewind.appspot.com/


参考资料:
http://webpy.org
http://www.189works.com/article-39148-2.html

https://developers.google.com/appengine/docs/python/gettingstartedpython27/


-------------------------------------------------------------上传中遇到的问题分割线---------------------------------------------------------------------------------

1、用的python2.7 在windows上,报了ssl认证错误

【appcfg.py update  --insecure helloworld/ 这样解决的,不过安全性就低了】

2、输入邮箱和密码时,报set attribute错误

【这是因为我的账号之前搭建google代理时,设置为2步登录验证的,所以这里密码要用动态生成的密码,https://accounts.google.com/IssuedAuthSubTokens#accesscodes这里可以生成】

3、报502认证错误

【这里是因为还在用google代理,去掉代理直接使用上传命令即可】

4、访问地址app地址提示服务器错误

【这里是因为没有加google代理,被墙了,再次使用google代理即可。真WS】


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上帝De助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值