Feb 19

其实也是一个很小的应用,需要借助js代码高亮脚本,可以到http://alexgorbatchev.com/wiki/SyntaxHighlighter:Download去下载

下载完,解压到一个新建的目录里面,然后新建一个core.py

 

#!/usr/bin/env python
import cgi
import datetime
import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp

Out ="""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Your code</title>
	<script type="text/javascript" src="scripts/shCore.js"></script>
	<script type="text/javascript" src="scripts/shBrushBash.js"></script>
	<script type="text/javascript" src="scripts/shBrushCpp.js"></script>
	<script type="text/javascript" src="scripts/shBrushCSharp.js"></script>
	<script type="text/javascript" src="scripts/shBrushCss.js"></script>
	<script type="text/javascript" src="scripts/shBrushDelphi.js"></script>
	<script type="text/javascript" src="scripts/shBrushDiff.js"></script>
	<script type="text/javascript" src="scripts/shBrushGroovy.js"></script>
	<script type="text/javascript" src="scripts/shBrushJava.js"></script>
	<script type="text/javascript" src="scripts/shBrushJScript.js"></script>
	<script type="text/javascript" src="scripts/shBrushPhp.js"></script>
	<script type="text/javascript" src="scripts/shBrushPlain.js"></script>
	<script type="text/javascript" src="scripts/shBrushPython.js"></script>
	<script type="text/javascript" src="scripts/shBrushRuby.js"></script>
	<script type="text/javascript" src="scripts/shBrushScala.js"></script>
	<script type="text/javascript" src="scripts/shBrushSql.js"></script>
	<script type="text/javascript" src="scripts/shBrushVb.js"></script>
	<script type="text/javascript" src="scripts/shBrushXml.js"></script>
	<link type="text/css" rel="stylesheet" href="styles/shCore.css"/>
	<link type="text/css" rel="stylesheet" href="styles/shThemeDefault.css"/>
	<script type="text/javascript">
		SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
		SyntaxHighlighter.all();
	</script>
</head>

<body >
<h1>Here is your Code</h1>
<p>Good Luck!</p>

<pre class="brush: %s;">
%s
</pre>
</html>
"""


class CodeStr(db.Model):
  content = db.TextProperty()
  date = db.DateTimeProperty(auto_now_add=True)
  lang = db.StringProperty(multiline=False)

class InputCode(webapp.RequestHandler):
  def get(self):
    self.response.out.write('<html><body>')
    self.response.out.write('Code:')

    self.response.out.write("""<form action="/sc" method="post">
                                    <div><select size="4" id= "lang" name="lang">
                                        <option>python</option>
                                        <option>js</option>
                                        <option>xml</option>
                                        <option>bash</option>
                                    </select></div>
                                    <div><textarea name="content" rows="8" cols="70"></textarea></div>
                                    <div><input type="submit" value="Submit"></div>
                                </form>
                            </body>""")


class SaveCode(webapp.RequestHandler):
  def post(self):
    self.code = CodeStr()
    self.code.content = self.request.get('content')
    self.code.lang = self.request.get('lang')
    self.code.put()
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write('Your code\'s ID is %s\n' % self.code.key().id())
    self.response.out.write('You can visit http://billgaesh.appspot.com/o?id=%s to get your code\n' % self.code.key().id())
    self.response.out.write('Good Luck')
  def get(self):
    self.response.out.write('Sorry This doesn\'t work!')

class OutCode(webapp.RequestHandler):
  def get(self):
    global Out
    id = self.request.get('id')
    key = db.Key.from_path("CodeStr", int(id))
    query = CodeStr.all()
    query.ancestor(key)
    code = query.get()
    s = code.content.replace('<', '&lt;').replace('>', '&gt;')
    O = Out % (code.lang, s)
    self.response.out.write(O)

application = webapp.WSGIApplication([
  ('/i', InputCode),
  ('/sc', SaveCode),
  ('/o', OutCode)
], debug=True)

def main():
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()


在新建一个叫app.yaml的文件

 

application: *
version: 13
runtime: python
api_version: 1

handlers:
- url: /i
  script: core.py

- url: /sc
  script: core.py

- url: /o
  script: core.py

- url: /src
  static_dir: src

- url: /scripts
  static_dir: scripts

- url: /styles
  static_dir: styles

- url: /
  static_files: test.html
  upload: test.html

 

 

把其中的

application: *

改成你自己注册的app名称,然后用GAE SDK上传到GAE的服务器上就可以了

在 http://*.appspot.com/i 里面输入代码,然后会给你一个ID,在http://*.appspot.com/o?id=** 里面就可以看到你的代码了

目前还比较垃圾,如果不满意的话,可以自行修改以下

http://billgaesh.appspot.com/i 是我的代码贴地址,欢迎使用

目前已知的BUG是如果代码中有“”“,可能会跨站……

Jan 16

一直想写个这样的程序,可苦于自己对于这个GAE一窍不通……所以一直也没下手写,今天终于写了一个,其实也十分简单,参照了一下网上的一个些用pyfetion收天气的方法

最关键的是这一部分(虽然很短)

monitor.py

#! /usr/bin/python
#coding=utf-8

import re
from google.appengine.api import mail
from google.appengine.api import urlfetch

wap = urlfetch.fetch('http://m.baidu.com/tq?area=%E5%9E%A6%E5%88%A9').content
Tomorrow = re.findall(r'明\:.*?<',wap)[0][:-1]

mail.send_mail(sender="******@gmail.com", to="******@139.com", subject="Weather", body=Tomorrow)

最初,因为不了解gae的详细用法,先写了一个通用版

 

#! /usr/bin/python

import urllib,re
import email,smtplib,time

wap = urllib.urlopen('http://m.baidu.com/tq?area=%E5%9E%A6%E5%88%A9').read().decode('utf-8') #网页视情况而定
Tomorrow = re.findall(r'明\:.*?<'.decode('gbk'),wap)[0][:-1]

msg = email.Message.Message()
msg['to']='billnowar@139.com'
msg['from']='billnowar@163.com'
msg['date']=time.ctime()
msg['subject']=email.Header.Header('weather','gb2312')
body=email.MIMEText.MIMEText(Tomorrow.encode('gb2312'),_subtype='plain',_charset='gb2312')

server=smtplib.SMTP('smtp server')
server.login('username','password')
server.sendmail(msg['from'],msg['to'],msg.as_string()[:-1]+body.as_string())

这上面这个是用python自带的库写的,学习了以下email和smtplib后很快就告成的版本

接下来就不容易了,我把email和smtplib、urllib改成了from google.appengine.api import mail和from google.appengine.api import urlfetch这两个……(因为不知到email和smtplib、urllib能不能用……)

然后又出现了编码问题……我不知道gae上到底该用utf-8还是别的什么,而且我也不清楚我手机是用什么编码(这个最麻烦)……

经过实验,发现根本不用改编码,全部都用utf8即可,百度天气是utf-8,所以省去了很多不必要的代码

忙活了两个多小时,终于还是让我搞成了!

通过写这个程序,初步了解了一下gae的机制:

1.GAE貌似可以使用所有的python自带库,既然re可以,那么其他的库理应也可以,这个没有实验,但是猜想应该是可以

2.GAE的cron,掌握了cron的基本写法,先写间隔 再写周期 再写具体时间

3.GAE编码用utf-8,这个实际有点废话……

4.GAE的google.appengine.api.mail.send_mail的sender参数应该添你的gmail邮箱,负责无法使用……