用GAE做代码贴

小马 posted @ 2010年2月19日 21:11 in 生活代码 with tags python pastebin GAE , 2197 阅读

其实也是一个很小的应用,需要借助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是如果代码中有“”“,可能会跨站……

KVS 7th Class Model 说:
2022年9月27日 21:07

The Kendriya Vidyalaya Sangathan has provided the KVS 7th Class Model Paper 2023 to all regional students studying at all states and union territories of the country, and the Upper primary education STD-7 sample model paper has provided with an important question to both medium student at their regional language of the school. KVS 7th Class Model Paper The Kendriya Vidyalaya Sangathan has provided the KVS 7th Class Model Paper 2023 to all regional students studying at all states and union territories of the country, and the Upper primary education STD-7 sample model paper has provided with an important question to both medium student at their regional language of the school.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter