Skip to content

Instantly share code, notes, and snippets.

@dz1984
Created April 14, 2013 02:49
Show Gist options
  • Save dz1984/5381173 to your computer and use it in GitHub Desktop.
Save dz1984/5381173 to your computer and use it in GitHub Desktop.
使用GAE去做跳板下載功能
#coding: utf8
import wsgiref.handlers;
from sgmllib import SGMLParser;
from google.appengine.ext import db;
from google.appengine.ext import webapp;
from google.appengine.api import users;
from google.appengine.api import urlfetch;
class Files(db.Model):
name = db.StringProperty();
content = db.BlobProperty();
encode = db.BooleanProperty();
pass;
class MainPage(webapp.RequestHandler):
def post(self):
urls = self.request.get('urls');
encode = self.request.get('encode');
if urls:
response = urlfetch.fetch(urls,method=urlfetch.GET);
if response.status_code == 200:
files = Files();
files.name = urls.split('/').pop() or 'None';
if encode:
exchange = (lambda x: chr((lambda h:(ord(h)%16)*16)(x)+(lambda l:(ord(l)/16))(x)));
files.content = db.Blob(''.join([exchange(i) for i in response.content]));
files.encode = True;
else:
files.content = db.Blob(response.content);
files.encode = False;
files.put();
pass;
self.redirect('/');
pass;
def get(self):
results = db.GqlQuery("SELECT * FROM Files");
context = [];
filesize =0;
strencode = (lambda b:(b) and '<font color="red">-Encode</font>' or '');
for result in results:
filesize += len(result.content);
context.append("""
<li><a href="/down?id=%s">%s</a>%s
<font size="2">(%d bytes) <a href="/clear?id=%s">Clear</a></font></li>
""" % (result.key(),result.name,strencode(result.encode),len(result.content),result.key()));
self.response.headers['Content-Type'] = 'text/html;charset=utf-8';
self.response.out.write(u"""
<html>
<head>
<title>跳板下載</title>
<style type=text/css>
#per {
text-indent: 2em;
}
</style>
</head>
<body>
<h1>為什麼會想寫跳板下載呢?那跳板下載又是什麼東西呢?</h1>
<p id='per'>主要原因是有次我想為Firefox找個好擴充套件,找了很久後,終於讓我物色到功能強大的套件,正興高采烈地下載,就到緊要關頭時,<b>公司的防火牆</b>跳出來擋了下來,沒轍~~~~</p>
<p>所以,決定利用Byte的高低位元互換,來躲過<b>公司的防火牆</b>嚴密地監控,最後,還是<b><i>Google</i></b>大顯神通,救了在下…<sub>Ending</sub></p>
<h3>輸入下載檔案的URL:</h3>
<form method='post' action='/'>
<input type='text' name='urls' size='100'>
<input type='checkbox' name='encode'>Encode
<input type='submit' value='Catch'>
</form>
<h3>已抓取檔案:</h3>
<ul>%s</ul>
<h2>Download files : %d</h2>
<b>Total files size: %d bytes</b>
<a href='/clear'>Clear All Data</a>
</body>
</html>
""" %("".join(context),results.count(),filesize));
pass;
pass;
class ClearData(webapp.RequestHandler):
def get(self):
user = users.get_current_user();
if user:
if users.is_current_user_admin():
fid = self.request.get('id');
if fid:
db.delete(db.get(fid));
else:
results = db.GqlQuery("SELECT * FROM Files");
for result in results:
db.delete(result);
pass;
pass;
self.redirect('/');
else:
self.response.headers['Content-Type'] = 'text/html';
self.response.out.write("""
<html>
<head><title>Error</title></head>
<body>
<h2>%s</h2>
<font color='red'>You aren\'t Administrator.</font><a href="/">Back</a>
<p><a href='%s'>Logout</a></p>
"""% (user.nickname(),users.create_logout_url('/')));
else:
self.redirect(users.create_login_url(self.request.uri));
pass;
pass;
class DownloadData(webapp.RequestHandler):
def get(self):
fid = self.request.get('id');
if fid:
files = db.get(fid);
mine = 'application/'+ files.name.split('.').pop();
disposition = 'filename=%s' % files.name;
self.response.headers['Content-Type'] = str(mine);
self.response.headers['Content-Disposition'] = str(disposition);
self.response.out.write(files.content);
pass;
else:
self.response.out.write('No file');
pass;
def main():
application = webapp.WSGIApplication([('/',MainPage),('/down',DownloadData),('/clear',ClearData)],debug=True);
wsgiref.handlers.CGIHandler().run(application);
pass;
if __name__ == '__main__':
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment