Skip to content

Instantly share code, notes, and snippets.

View mengzhuo's full-sized avatar

Meng Zhuo mengzhuo

View GitHub Profile
@mengzhuo
mengzhuo / Roulette_wheel_select.py
Created October 31, 2014 01:22
Roulette_wheel_select base on Python
def roulette_wheel_select(groups):
"""
Groups should format in list:
(weigth (positive), content)
More weight more chance that we pick this content
BUT not every time
i.e.
[(1, '1'),
@mengzhuo
mengzhuo / .zh_alias
Created October 27, 2014 03:53
简易Debian中文管理别名
# 新手专用 :)
countdown()
(
IFS=:
set -- $*
secs=$(( ${1#0} ))
while [ $secs -gt 0 ]
do
sleep 1 &
@mengzhuo
mengzhuo / reload
Created October 16, 2014 11:45
Auto reload by inotify
#!/bin/sh
# Can reload after file changed/modified/created/deleted
echo "Auto reload"
while true; do
ps aux | grep start.py | awk '{print $2}' | xargs kill > /dev/null
$VIRTUAL_ENV/bin/python $PWD/start.py -c ~/config.ini &
@mengzhuo
mengzhuo / conf.py
Created September 2, 2014 13:47
configure module (reloadable)
#!/usr/bin/env python
# encoding: utf-8
import os
from ConfigParser import SafeConfigParser
PREFIX = 'ZTY'
setting = SafeConfigParser()
setting.read(['/etc/zty-site.conf',
'~/zty-site.conf'])
#!/usr/bin/env python
# encoding: utf-8
def binary2grey(n):
return n >> 1 ^ n
def grey2binary(g):
mask = g >> 1
while mask:
g = g ^ mask
@mengzhuo
mengzhuo / gate.py
Last active August 29, 2015 14:05
Gate.py
#!/usr/bin/env python
# encoding: utf-8
import signal
import logging
logger = logging.getLogger(__name__)
import sys
import os
import multiprocessing
@mengzhuo
mengzhuo / check_tls.py
Created August 1, 2014 13:52
Check STARTTLS status of given domain
#!/usr/bin/env python
# encoding: utf-8
"""
The MIT License (MIT)
Copyright (c) <2014> <Meng Zhou [email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@mengzhuo
mengzhuo / api.py
Created July 23, 2014 06:48
SImple API echo
from bottle import route, run
@route('/echo')
def echo():
return {'status':'ok'}
run(host='0.0.0.0', port=8998, reloader=False,
reloader=False, quiet=True, server='gevent')
@mengzhuo
mengzhuo / checker.py
Created July 16, 2014 05:32
Python Port conflict checker
def check_port_conflict(port, show_logging=True):
"""Check whether Shadowsock bind port conflicted with services
registered in services database from Linux /etc/services
If conflicted, show logging warning and return tuple that conflicted
else, return None
:port: int bind port (TCP/UDP)
:returns: None/tuple that conflicted
"""
@mengzhuo
mengzhuo / test_sendmail.py
Created July 8, 2014 10:11
Async Task Queue like base on greenlets
from gevent import monkey
monkey.patch_all()
from dns import resolver
from tuck import tuck
def get_mail_server(receiver=''):
user, domain = receiver.split('@')
dest_list = sorted([x.to_text().strip('.').split(' ')