GAE1.4尝鲜
GAE1.4发布了,带来了很多新的功能。不过我最在意的有两个:一是支持代码下载;二是支持及时通讯(channel api),现在我们来玩玩这两个东西。
1、代码下载
我们一般通过appcfg.py来上传、更新代码,但是没有下载的功能。如果换了一台机器,又看到不代码,真的很窝火。不过,现在GAE提供了,真的很方便,赞一个GAE。下载代码很简单,看一个简单的例子:
Usage: appcfg.py [options] download_app -A app_id [ -V version ] <out-dir>
C:\Users\Administrator\Desktop>appcfg.py download_app -A flyingzl -V 2 flyingzl-projectD:\program\python\GAE\appcfg.py:42: DeprecationWarning: the sha module is deprecated; use the hashlib module instead os.path.join(DIR_PATH, 'lib', 'django'),D:\program\python\GAE\google\appengine\tools\dev_appserver_login.py:33: DeprecationWarning: the md5 module is deprecated; use hashlib instead import md5Server: appengine.google.com.Fetching file list...Fetching files...[1/23] chat.py[2/23] send_mail.py[3/23] hello_template.html[4/23] hello_user.py...
#coding=utf-8'''Created on 2010-12-4@author: flyingzl'''from google.appengine.ext import webappfrom google.appengine.ext.webapp import util,templatefrom google.appengine.api import channelfrom hashlib import md5import timeclass Chat(webapp.RequestHandler): def get(self): self.response.headers['Content-Type']='text/html;charset=utf-8' key=md5('1234567890').hexdigest() client_id=channel.create_channel(key) self.response.out.write(template.render("templates/chat.html", {'channel_id':client_id})) class ChatSender(webapp.RequestHandler): def get(self): self.response.headers['Content-Type']='text/html;charset=utf-8' self.error(500) self.response.out.write('只支持Post请求') def post(self): key=md5('1234567890').hexdigest() message=self.request.get('message','') now=time.strftime("%Y-%m-%d %H:%M:%S") ip=self.request.environ['REMOTE_ADDR'] try: channel.send_message(key, "%s(%s)-->%s"%(ip,now,message)) except channel.InvalidChannelClientIdError: self.error(500) self.response.out.write("Channel标识符不合法") except channel.InvalidMessageError: self.error(500) self.response.out.write("发送的消息太长,最大长度不能超过")+channel.MAXIMUM_MESSAGE_LENGTH+"个字节" def main(): app=webapp.WSGIApplication([('/chat',Chat),('/chat/sender',ChatSender)],debug=True); util.run_wsgi_app(app)if __name__=='__main__': main()
<!doctype><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta><title>Chat</title><style type="text/css">#mainDiv{width:600px;height:400px;padding:5px;margin:10px auto;border:1px solid lightblue;}#message{width:100%;height:370px;margin-bottom:5px;}#userMessage{width:450px;margin-right:2px;}</style></head><body><div id="mainDiv"><textarea id='message'></textarea><input id="userMessage" /><button id='btn' disabled onClick="sendMessage()">正在连接服务器……</button></div><script type="text/javascript" src="/_ah/channel/jsapi"></script><script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script><script>var channel_id='{{channel_id}}',timeoutID='';$(function(){var channel=new goog.appengine.Channel(channel_id),socket=channel.open();socket.onopen=function(){$('#btn').html("发送").removeAttr('disabled');}socket.onmessage=function(data){var message=$("#message").val();$.trim(message)?$("#message").val(message+"\n"+data.data):$("#message").val(data.data);}});function sendMessage(message){if(timeoutID){alert("您发送的太快了,休息一下下……");return;}var message=$('#userMessage').val();if(!$.trim(message)){alert("请输入要发送的消息!");$('#userMessage').focus();return;}$.ajax({type:'POST',data:"message="+message,url:'/chat/sender',error:function(err){alert(err.responseText);}});$('#userMessage').val('')timeoutID=setTimeout(function(){timeoutID='';},500);}</script></body></html>