Skip to content

Instantly share code, notes, and snippets.

@xiaowl
xiaowl / pyscp.py
Created July 9, 2012 10:20
Pure python implementation of `scp`, using SFTP protocol. Depends on paramiko.
def scp(sshclient, src, dest=''):
path = os.path.abspath(os.path.expanduser(src)).rstrip('/')
basename = os.path.basename(path)
ftp = sshclient.open_sftp()
if os.path.isdir(path):
for root, dirs, files in os.walk(path):
root = root.rstrip('/')
dirname = os.path.basename(root)
if root != path:
dirname = os.path.join(basename, root.split(path)[1].strip('/'))
@xiaowl
xiaowl / pyssh.py
Created July 9, 2012 10:22
ssh login using paramiko.
def ssh(hostname, keyfile, username='ubuntu'):
host_key_file = os.path.expanduser('~/.ssh/known_hosts')
pkey = paramiko.RSAKey.from_private_key_file(os.path.expanduser(keyfile))
client = paramiko.SSHClient()
client.load_system_host_keys()
client.load_host_keys(host_key_file)
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username=username, pkey=pkey)
return client
@xiaowl
xiaowl / gist:3192384
Created July 28, 2012 08:08
RGB to Hex
function rgb2hex(r, g, b){
var hex = r << 16 | g << 8 | b;
return (0x1000000 | hex).toString(16).substring(1); // prepend leading zeros.
}
@xiaowl
xiaowl / tools.md
Created September 9, 2012 07:19
A collection of useful tools

Supervisor

http://supervisord.org/

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.

It shares some of the same goals of programs like launchd, daemontools, and runit. Unlike some of these programs, it is not meant to be run as a substitute for init as “process id 1”. Instead it is meant to be used to control processes related to a project or a customer, and is meant to start like any other program at boot time.

@xiaowl
xiaowl / README
Created September 13, 2012 12:18 — forked from mbox/Dualcache
Two-level cache for Django
To use the two-layer cache:
* Run memcached on each Django webserver
* Add "LOCAL_CACHE_ADDR = ('127.0.0.1:11211',)" to settings.py
* Replace "from django.core.cache import cache" with "from dualcache import cache" everywhere
you want to use the two-layer cache. It can be freely mix and matched with Django's default caching.
@xiaowl
xiaowl / redis_session_backend.py
Created September 18, 2012 10:15 — forked from mikeyk/redis_session_backend.py
A redis backend for Django Sessions, tested on Django 1.3+
from django.contrib.sessions.backends.base import SessionBase, CreateError
from django.conf import settings
from django.utils.encoding import force_unicode
import redis
class SessionStore(SessionBase):
""" Redis store for sessions"""
def __init__(self, session_key=None):
self.redis = redis.Redis(
input = gets.chomp
continuous_bye = 0
while true:
if input == "BYE":
continuous_bye += 1
else
@xiaowl
xiaowl / gist:3917119
Created October 19, 2012 09:17
Kill Baidu music player's title roll effect(Marquee)
player.setEventListener(player.EVENTS.STATECHANGE, function(j) { playCtrl.changeTitleRoll("stop"); });
@xiaowl
xiaowl / gist:3944649
Created October 24, 2012 07:51
use inotifywait to trigger some operation
#!/bin/bash
while true; do
inotifywait -e create /path/to/watch && script.sh
done
@xiaowl
xiaowl / Functools.java
Created December 17, 2012 09:29
Functools is a helper class to introduce the power of FP to the heavy Java world..
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Functools {