Skip to content

Instantly share code, notes, and snippets.

def run_with_cgi(application):
environ = dict(os.environ.items())
environ['wsgi.input'] = sys.stdin
environ['wsgi.errors'] = sys.stderr
environ['wsgi.version'] = (1, 0)
environ['wsgi.multithread'] = False
environ['wsgi.multiprocess'] = True
environ['wsgi.run_once'] = True
def simple_app(environ, start_response):
"""Simplest possible application object"""
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return ['Hello world!\n']
import os, sys
from bottle import Bottle
#!/usr/bin/python
#coding: utf-8
import re
class MappingFileError(Exception): pass
class SimpleParser(object):
"""Callable to turn path expressions into regexes with named groups.
#from bottle src
class ConfigDict(dict):
''' A dict-subclass with some extras: You can access keys like attributes.
Uppercase attributes create new ConfigDicts and act as name-spaces.
Other missing attributes return None. Calling a ConfigDict updates its
values and returns itself.
>>> cfg = ConfigDict()
>>> cfg.Namespace.value = 5
class ConfigDict(dict):
''' A dict-subclass with some extras: You can access keys like attributes.
Uppercase attributes create new ConfigDicts and act as name-spaces.
Other missing attributes return None. Calling a ConfigDict updates its
values and returns itself.
>>> cfg = ConfigDict()
>>> cfg.Namespace.value = 5
>>> cfg.OtherNamespace(a=1, b=2)
>>> cfg
@shiweifu
shiweifu / tmpl.py
Created April 2, 2013 06:25
bottle template
import functools
from urllib2 import *
TEMPLATE_PATH = ['./', './views/']
TEMPLATES = {}
#------------------------------------------utils------------------------------------------
def tob(s, enc='utf8'):
return s.encode(enc) if isinstance(s, unicode) else bytes(s)
@shiweifu
shiweifu / fork process by Python
Created September 5, 2012 03:25
fork process
#!/usr/bin/env python
#coding: utf-8
#author: shiweifu
#mail : [email protected]
import os
def fork_process(cmd, args, env):
child_pid = os.fork()
@shiweifu
shiweifu / randstr
Created August 31, 2012 10:04
random generator a string
def randstr(l):
s = ["chr(randint(65, 90))", #A-Z
"chr(randint(97, 122))", #a-z
"str(randint(0,9))"] #0-9
return "".join([eval(s[randint(0, len(s)-1)]) for x in xrange(l)])
@shiweifu
shiweifu / gen_time.c
Created August 28, 2012 03:05
test_time
#include <stdio.h>
#include <time.h>
int main(int argc, const char *argv[])
{
/*time_t t = time(NULL);*/
time_t t = 1346122873;
printf("%s\n", ctime(&t));
return 0;
function isUrl(s) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s);
}