Skip to content

Instantly share code, notes, and snippets.

-- author: shiweifu
-- mail: [email protected]
cjson = require("cjson")
function load_json_db( db_name )
local f = io.open(db_name)
local s = f:read("*a")
f:close()
return cjson.decode(s)
@shiweifu
shiweifu / lc
Created July 21, 2012 13:53
lc
LANG="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_CTYPE="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_ALL=
@shiweifu
shiweifu / upload.lua
Created July 27, 2012 02:40
通过openresty 模块上传文件,并在上传的过程中进行md5计算
local upload = require "resty.upload"
local cjson = require "cjson"
local resty_md5 = require "resty.md5"
local chunk_size = 4096 -- should be set to 4096 or 8192
-- for real-world settings
local form = upload:new(chunk_size)
local data = ""
@shiweifu
shiweifu / add_message.py
Created August 10, 2012 08:07
test curl
#!/usr/bin/env python
import os
import sys
#cmd = """curl http://localhost:8082/apiv1/msgadmin --data \"title=%s&content=content&desc=%s&icon=http://a1.phobos.le/b7/3f/8b/mzi.hztheuez.100x100-75.jpg&type=1&action=add&type_data={\"data\": {\"sort\": \"1\", \"title\": \"Naruto\", \"vip\": \"0\", \"tag\": \"pp_naruto\", \"icon\": \"http://a1.phobos.apple.com/us/r1000/010/Purple/b7/3f/8b/mzi.hztheuez.100x100-75.jpg\"}, \"content_type\": 1}&appname=%s\"}\"}\""""
cmd = """curl http://localhost:8082/apiv1/msgadmin --data \"title=%s&content=content&desc=%s&icon=http://a1.phobos.le/b7/3f/8b/mzi.hztheuez.100x100-75.jpg&type=1&action=add&type_data={\\\"\"data\\\"\": {\\\"\"sort\\\"\": \\\"\"1\\\"\", \\\"\"title\\\"\": \\\"\"Naruto\\\"\", \\\"\"vip\\\"\": \\\"\"0\\\"\", \\\"\"tag\\\"\": \\\"\"pp_naruto\\\"\", \\\"\"icon\\\"\": \\\"\"http://a1.phobos.apple.com/us/r1000/010/Purple/b7/3f/8b/mzi.hztheuez.100x100-75.jpg\\\"\"}, \\\"\"content_type\\\"\": 1}&appname=%s&lang=ja\""""
@shiweifu
shiweifu / obj2json
Created August 22, 2012 02:12
js_obj2string
function objToString (obj) {
var str = '';
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
str += p + '::' + obj[p] + '\n';
}
}
return str;
}
function isUrl(s) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s);
}
@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;
@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 / 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 / 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)