Skip to content

Instantly share code, notes, and snippets.

@ls0f
ls0f / gist:9222088
Created February 26, 2014 02:10
login_required for tornado
def login_required(meth):
def deal(self):
if not self.get_secure_cookie("user"):
self.redirect("/user_login/")
return
return meth(self)
return deal
class MainHandler(tornado.web.RequestHandler):
@ls0f
ls0f / gist:8a5cf72d881199852ce5
Created December 3, 2014 13:39
python optparse
# coding:utf-8
# opt parse
from optparse import OptionParser
class Test():
def sum(self,*args):
print "sum method"
print "result is",sum(args)
@ls0f
ls0f / gist:b80e3f3dbb5d9ad61797
Created December 3, 2014 14:10
python unittest
import unittest
class Test():
def sub(self,*args):
return sum(args)
def bigThan5(self,x):
if x>5:
@ls0f
ls0f / gist:8be68a3266601f872832
Last active August 29, 2015 14:11
tornado hack static url in template html
def static_url_patch():
from tornado.template import Template
old_generate = Template.generate
def hack_generate(self,**kwargs):
t = old_generate(self,**kwargs)
import re
print "hack generate"
t = re.sub(r'(?<=[\'|"])/static/',r'http://example.com/static/',t)
return t
@ls0f
ls0f / rust guess game
Created March 5, 2015 16:04
rust guess game
use std::io;
use std::rand;
use std::cmp::Ordering;
fn cmp(a: u32, b: u32) -> Ordering {
if a < b { Ordering::Less }
else if a > b { Ordering::Greater }
else { Ordering::Equal }
}
@ls0f
ls0f / tornado-redis-cache
Created April 27, 2015 01:34
tornado-redis-cache
#coding:utf-8
from tornado.web import RequestHandler
import redis
def getRedisObj(rdb=0):
pool = redis.ConnectionPool(
host='127.0.0.1', password='', port=6379, db=rdb, socket_timeout=3)
r = redis.Redis(connection_pool=pool)
@ls0f
ls0f / block-ip.py
Last active August 29, 2015 14:20
block ip
#coding: utf-8
import os
## block cc攻击 的ip
CMD = "netstat -ntup | grep -v SYN |awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr"
MAX_CONN = 50
@ls0f
ls0f / lua
Created August 6, 2015 18:23
lua-yeild-http
#!/usr/local/bin/lua
local socket = require "socket"
function receive(c)
c:settimeout(0) --> 不阻塞
local s,status,partial = c:receive(2^10)
if status == "timeout" then
coroutine.yield(c)
end
@ls0f
ls0f / gist:85817aaedbabea7e6cf6
Created August 6, 2015 18:27
lua-producer-consumer
#!/usr/local/bin/lua
function producer()
return coroutine.create(function()
while true do
local line = io.read()
coroutine.yield(line)
end
end)
end
@ls0f
ls0f / python-producer-consumer.py
Last active August 29, 2015 14:26
python-producer-consumer
import sys
def producer():
while True:
line = sys.stdin.readline()
yield line
def consumer(p):
line = p.next()