首页 > 编程知识 正文

在GAEGoogle App Engine上搭建python27的webpy程序

时间:2023-05-05 04:51:06 阅读:211427 作者:189

资源:
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: helloworldversion: 1runtime: python27api_version: 1threadsafe: nohandlers:- url: /.* script: helloworld.py6、启动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、复制Libsite-packagesweb安装目录到helloworld目录下
2、修改helloworl.py的内容如下:
#encoding: utf-8import weburls = ('/(.*)', '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-8import webrender = 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】


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。