Skip to content

Instantly share code, notes, and snippets.

@mickhan
mickhan / logger_setting.py
Created December 10, 2015 02:48
配置一个logger
View logger_setting.py
#日志
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 / timeout.py
Created December 10, 2015 02:44
python timeout 模块。用于结束执行时间过长的函数。来自http://stackoverflow.com/questions/2281850/timeout-function-if-it-takes-too-long-to-finish。
View timeout.py
#!/usr/bin/python
#coding=utf-8
from functools import wraps
import errno
import os
import signal
class TimeoutError(Exception):
pass
View gist:8119732
#!/bin/bash
# Copyright 2013 Percona LLC and/or its affiliates
set -e
if [ -z "$3" ]; then
echo "rename_db <server> <database> <new_database>"
exit 1
fi
db_exists=`mysql -h $1 -e "show databases like '$3'" -sss`
if [ -n "$db_exists" ]; then
echo "ERROR: New database already exists $3"
View gist:6006313
/**
* 对日期进行格式化,
* @param date 要格式化的日期
* @param format 进行格式化的模式字符串
* 支持的模式字母有:
* y:年,
* M:年中的月份(1-12),
* d:月份中的天(1-31),
* h:小时(0-23),
* m:分(0-59),
@mickhan
mickhan / gist:5829436
Created June 21, 2013 07:09
nginx /etc/init.d 脚本
View gist:5829436
#! /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
@mickhan
mickhan / gist:5829386
Created June 21, 2013 06:59
在shell脚本中修改crontab
View gist:5829386
crontab -l > cron.tmp
echo "0 0 */1 * * /path/to/script.sh" >> cron.tmp
crontab cron.tmp
rm -f cron.tmp
@mickhan
mickhan / gist:5720727
Created June 6, 2013 10:52
tornado下载文件
View gist:5720727
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:5711515
Created June 5, 2013 03:59
记录文件日志,每天midnight创建新文件
View gist:5711515
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:5683459
Last active December 17, 2015 22:39
jquery ajax
View gist:5683459
<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:5676751
Created May 30, 2013 09:26
python pidfile
View gist:5676751
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: