Skip to content

Instantly share code, notes, and snippets.

@mickhan
mickhan / timeout.py
Created December 10, 2015 02:44
python timeout 模块。用于结束执行时间过长的函数。来自http://stackoverflow.com/questions/2281850/timeout-function-if-it-takes-too-long-to-finish。
#!/usr/bin/python
#coding=utf-8
from functools import wraps
import errno
import os
import signal
class TimeoutError(Exception):
pass
@mickhan
mickhan / logger_setting.py
Created December 10, 2015 02:48
配置一个logger
#日志
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
#app handler's log
handler_logger = logging.getLogger('handler')
handler_logger.setLevel(logging.DEBUG)
handler_fh = logging.FileHandler(log_dir+'/handler.log')
handler_fh.setLevel(logging.DEBUG)
handler_fh.setFormatter(formatter)
handler_logger.addHandler(handler_fh)
@mickhan
mickhan / redis-ve
Created January 31, 2013 04:01 — forked from pterk/redis-ve
#!/bin/bash
VERSION="2.6.2"
ARCHIVE=redis-${VERSION}.tar.gz
if [ -z "${VIRTUAL_ENV}" ]; then
echo "Please activate a virtualenv first";
exit
fi
def txt2html(txt):
'''将txt以行为单位加上<li></li>标签'''
def escape(txt):
'''将txt文本中的空格、&、<、>、(")、(')转化成对应的的字符实体,以方便在html上显示'''
txt = txt.replace('&','&#38;')
txt = txt.replace(' ','&#160;')
txt = txt.replace('<','&#60;')
txt = txt.replace('>','&#62;')
txt = txt.replace('"','&#34;')
txt = txt.replace('\'','&#39;')
@mickhan
mickhan / gist:5676751
Created May 30, 2013 09:26
python pidfile
import os
import sys
pid = str(os.getpid())
pidfile = "/tmp/pidfilename.pid"
if os.path.isfile(pidfile):
print "%s already exists, exiting" % pidfile
sys.exit()
else:
@mickhan
mickhan / gist:5683459
Last active December 17, 2015 22:39
jquery ajax
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: "/ajax_url",
type: "POST",
data: {
"dataname": data
},
dataType: "json",
success:function(data_info){
@mickhan
mickhan / gist:5711515
Created June 5, 2013 03:59
记录文件日志,每天midnight创建新文件
import config
import logging
from logging.handlers import TimedRotatingFileHandler
logger = logging.getLogger(config.LOG_NAME)
logfile = '%s/%s' % (config.LOG_BASE_PATH, config.LOG_FILENAME)
fileTimeHandler = TimedRotatingFileHandler(logfile, "midnight", 1)
fileTimeHandler.suffix = "%Y%m%d.log" # 设置切分后日志文件名格式
@mickhan
mickhan / gist:5720727
Created June 6, 2013 10:52
tornado下载文件
filename = "%s_attack_report.xls" % file_to_download
f = open(filename, "r")
self.set_header('Content-Type', 'text/csv')
self.set_header('Content-Disposition', 'attachment; filename=' + filename + '')
self.write(f.read())
@mickhan
mickhan / gist:5829386
Created June 21, 2013 06:59
在shell脚本中修改crontab
crontab -l > cron.tmp
echo "0 0 */1 * * /path/to/script.sh" >> cron.tmp
crontab cron.tmp
rm -f cron.tmp
@mickhan
mickhan / gist:5829436
Created June 21, 2013 07:09
nginx /etc/init.d 脚本
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all