Google App Engine 入门: Hello World
(本文译自:Google App Engine Getting Started)
Google App Engine 应用通过 CGI 标准协议与服务器通讯.这是一个标准的Http处理流程,Web服务接受到客户端发来的Get或Post请求,web服务器把请求转发给你的应用程序,由应用程序来处理要输出的内容。
为了更好的理解这个过程,下面就开始开发我们经典的Hellow World应用程序吧。在这一章,仅仅只是实现显示一些简单的信息的功能。
创建一个简单的 Request Handler
首先创建一个名为 helloworld 的文件夹。 除非特殊说明,以后所有关于这个应用程序的文件都将放在这个文件夹里面。
在helloworld 文件夹里, 创建一个新文件 helloworld.py,文件内容如下:
print 'Content-Type: text/plain' print '' print 'Hello, world!'
这个Python 脚本处理一个request请求,并且设置一个Http header,输出一个空行和一段信息 Hello, world!.
创建配置文件
每个App Engine application 都包含一个名为 app.yaml的配置文件。 在这个配置文件中,可以设置具体的某个URL需要用哪个Python脚本来处理.
现在,在 helloworld 文件夹中,创建一个新的 app.yaml 文件,输入以下内容:
application: helloworld version: 1 runtime: python api_version: 1 handlers: - url: /.* script: helloworld.py
这个配置文件描述了以下内容::
- 这个应用程序的标识是
helloworld. 这个标识需要和你在App Engine网站上创建的应用程序标识保持一致。在开发期间你可以使用任何你喜欢的名字,但是上传的时候,必须要和你在App Engine 注册的标识保持一致。现在,我们把它设置为helloworld. - 你的应用程序的版本号为
1,如果你在上传应用之前修改了这个编号, App Engine 将会自动保留前一个版本的副本,以方便你可以在管理平台中将当前版本恢复成原来的版本。 - 该应用运行在
python环境, 环境版本是 1. 目前只有Python可选,将来会提供更多的运行环境和开发语言. - 所有符合正则表达式
/.*(所有URL) 的请求,都由helloworld.py脚本来处理.
该配置文件使用 YAML语法. 关于该配置文件的更多选项, 请参考 the app.yaml reference.
测试应用程序
现在这个应用程序已经基本上完整了。 你可以在本地App Engine SDK环境中进行模拟运行测试。
首先,指定应用路径为 helloworld 目录,使用下面的命令启动测试环境Web服务程序,:
google_appengine/dev_appserver.py helloworld/
这个Web服务程序将监听8080端口. 你可以在浏览器中输入以下地址进行测试:
关于这个web服务程序的更多选项(如怎样修改默认端口等), 请查看the Dev Web Server reference, 或者使用命令行选项 --help.
无需中断你的开发
在开发过程中,你不需要不停的重启Web服务程序。Web服务程序可以自行判断哪些脚本文件已经被修改过了,并且重新加载这些脚本。
试一试: 不要关闭web服务程序, 编辑 helloworld.py 将 Hello, world! 修改成其他内容. 重新访问http://localhost:8080/ ,看看是不是您的修改已经生效了!
要关闭Web服务程序,您只需在控制台中按下 Control-C (或其他有效的 "break" 功能键).
PS:在以后的入门指导中,你可以让你的Web服务程序一直开着 。
接下来...
我们已经开发了一个完整的web应用程序!你可能迫不及待的就想把这个程序发布出去,并分享给你的朋友了。且慢,还是让我们给它增加一些功能后再这样做吧,毕竟它太简陋了。
下一章: 使用 Webapp Framework.
怎么再能不被封呢
我是菜鸟,请问一下在哪运行google_appengine/dev_appserver.py helloworld/命令?
Thx!
对徐大很赞~
有两点:
1.app.yaml的最后一行前面应该有两个空格的,老大应该及时改回来
2.google_appengine/dev_appserver.py helloworld/这条命令没解释清楚,首先如果在Windows系统下,命令格式不对,'/'应该改为'\';还有关于路径,你能保证,人家的文件夹名就是google_appengine,;helloworld应该有一个完整路径才对,比如对我来说,这条命令应该长这样的:C:\Python25\python D:\Google\dev_appserver.py D:Google\helloworld\ ,虽然这其中还涉及环境变量什么的,写法本可以更简单,但是你应该吧这些都说清楚,对新手来说,确实很容易在这些问题上出错,打击了人家的信心就不好啦
总之,对老大很赞,继续学习中:-)
对徐大很赞~
有两点:
1.app.yaml的最后一行前面应该有两个空格的,老大应该及时改回来
2.google_appengine/dev_appserver.py helloworld/这条命令没解释清楚,首先如果在Windows系统下,命令格式不对,'/'应该改为'\';还有关于路径,你能保证,人家的文件夹名就是google_appengine,;helloworld应该有一个完整路径才对,比如对我来说,这条命令应该长这样的:C:\Python25\python D:\Google\dev_appserver.py D:Google\helloworld\ ,虽然这其中还涉及环境变量什么的,写法本可以更简单,但是你应该吧这些都说清楚,对新手来说,确实很容易在这些问题上出错,打击了人家的信心就不好啦
总之,对老大很赞,继续学习中:-)
python~语法很简洁
help!
c:\Program Files\Google\google_appengine\micolog>dev_appserver.py ..\micolog
...
WARNING ...datestore_file_stub.py] Could not read datastore data from
c:\docume~1\admin~1locals~1\temp\dev_appserver.datastore
WARNING ........................\dev_appserver.datastore.history
INFO dev_appserver_main.py] Running application powerpirates on port 8080:
http://localhost:8080
thanks!
appcfg.py:40: DeprecationWarning: the sha module is d
eprecated; use the hashlib module instead
Could not guess mimetype for images/favicon.ico. Using application/octet-stream
上传不成功怎么回事呢
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\dev_appserv
execfile(script_path, globals())
File "C:\Program Files\Google\google_appengine\google\appe
erver_main.py", line 358, in
sys.exit(main(sys.argv))
File "C:\Program Files\Google\google_appengine\google\appe
erver_main.py", line 306, in main
config, matcher = dev_appserver.LoadAppConfig(root_path,
File "C:\Program Files\Google\google_appengine\google\appe
erver.py", line 2638, in LoadAppConfig
raise AppConfigNotFoundError
google.appengine.tools.dev_appserver.AppConfigNotFoundError
你好,我的不报任何错误,可是运行浏览器之后没有任何结果,直接找不到服务器
这是怎么回事呀?
[root@localhost google_appengine]# ./appcfg.py update test/
./appcfg.py:40: DeprecationWarning: the sha module is deprecated; use the hashlib module instead
DIR_PATH,
Traceback (most recent call last):
File "./appcfg.py", line 55, in
execfile(script_path, globals())
File "/tmp/google_appengine/google/appengine/tools/appcfg.py", line 1943, in
main(sys.argv)
File "/tmp/google_appengine/google/appengine/tools/appcfg.py", line 1936, in main
AppCfgApp(argv).Run()
File "/tmp/google_appengine/google/appengine/tools/appcfg.py", line 1521, in Run
self.action.function(self)
File "/tmp/google_appengine/google/appengine/tools/appcfg.py", line 1726, in Update
rpc_server = self._GetRpcServer()
File "/tmp/google_appengine/google/appengine/tools/appcfg.py", line 1649, in _GetRpcServer
save_cookies=self.options.save_cookies)
File "/tmp/google_appengine/google/appengine/tools/appcfg.py", line 125, in __init__
self.opener = self._GetOpener()
File "/tmp/google_appengine/google/appengine/tools/appcfg.py", line 337, in _GetOpener
opener.add_handler(urllib2.HTTPSHandler())
AttributeError: 'module' object has no attribute 'HTTPSHandler'
./appcfg.py:40: DeprecationWarning: the sha module is deprecated; use the hashlib module instead
DIR_PATH,
Error parsing yaml file:
Unexpected attribute 'pplication' for object of type .
in "test/app.yaml", line 1, column 13
徐大大可是以前HOT-BBS站长?
001over
非常感谢啊!我是看着你翻译的教程一步步入门的,我现在会一些简单设置并且上传程序了,我刚刚还绑定了域名,也绑定成功了,可是却自动给我添加了www,不加www就不能访问,而不是像你这样的裸体域名。请问是怎么做的?谢谢,等待回复……
E:\Google\helloworld>dev_appserver.py .
INFO 2009-02-19 07:59:28,619 appengine_rpc.py] Server: appengine.google.com
INFO 2009-02-19 07:59:28,619 appcfg.py] Checking for updates to the SDK.
INFO 2009-02-19 07:59:28,901 appcfg.py] The SDK is up to date.
WARNING 2009-02-19 07:59:28,901 datastore_file_stub.py] Could not read datastor
e data from c:\docume~1\fuhao\locals~1\temp\dev_appserver.datastore
WARNING 2009-02-19 07:59:28,901 datastore_file_stub.py] Could not read datastor
e data from c:\docume~1\fuhao\locals~1\temp\dev_appserver.datastore.history
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\dev_appserver.py", line 60, in
run_file(__file__, globals())
File "C:\Program Files\Google\google_appengine\dev_appserver.py", line 57, in
run_file
execfile(script_path, globals_)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_apps
erver_main.py", line 463, in
sys.exit(main(sys.argv))
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_apps
erver_main.py", line 442, in main
static_caching=static_caching)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_apps
erver.py", line 3168, in CreateServer
return BaseHTTPServer.HTTPServer((serve_address, port), handler_class)
File "C:\Python25\lib\SocketServer.py", line 330, in __init__
self.server_bind()
File "C:\Python25\lib\BaseHTTPServer.py", line 101, in server_bind
SocketServer.TCPServer.server_bind(self)
File "C:\Python25\lib\SocketServer.py", line 341, in server_bind
self.socket.bind(self.server_address)
File "", line 1, in bind
socket.error: (10013, 'Permission denied')
E:\Google\helloworld>
报了一个权限错误,不知道咋回事,在此来请教了。
我的报错如下:
File "",line 1
google_appengine>dev_appserver.py helloworld/(字母d下面有一个尖尖)
请问怎么改这个错误啊?
哇塞 终于输出helloworld了 两个空格 这下记住了 !
只是warning应该没问题的,检查一下自己的代码吧
d:\Program Files\Google\google_appengine>dev_appserver site/
INFO 2009-01-23 16:51:45,334 appcfg.py] Server: appengine.google.com
INFO 2009-01-23 16:51:45,345 appcfg.py] Checking for updates to the SDK.
WARNING 2009-01-23 16:51:45,940 datastore_file_stub.py] Could not read datastor
e data from c:\users\admin\appdata\local\temp\dev_appserver.datastore
WARNING 2009-01-23 16:51:45,944 datastore_file_stub.py] Could not read datastor
e data from c:\users\admin\appdata\local\temp\dev_appserver.datastore.history
WARNING 2009-01-23 16:51:45,956 dev_appserver.py] Could not initialize images A
PI; you are likely missing the Python "PIL" module. ImportError: No module named
PIL
INFO 2009-01-23 16:51:45,969 dev_appserver_main.py] Running application fant
seer on port 8080: http://localhost:8080
http://localhost:8080在浏览器里无法显示页面啊
还有“datastore_file_stub.py] Could not read datastor
e data from c:\users\admin\appdata\local\temp\dev_appserver.datastore”
这是什么问题啊
新手一定要注意最后一行script前加两个空格