Skip to content

Instantly share code, notes, and snippets.

View allanlei's full-sized avatar
:shipit:
What's up?

Allan Lei allanlei

:shipit:
What's up?
View GitHub Profile
@allanlei
allanlei / app.yml
Created September 13, 2013 07:41
Google Appengine config for handling default page for a directory (index.html)
handlers:
- url: /(.*\..+)
static_files: static/\1
upload: static/(.*)
- url: /(.*)
static_files: static/\1/index.html
upload: static/(.*)
@allanlei
allanlei / random_port.py
Created December 23, 2013 03:23
Generates a random (assumed available) port
import socket
def random_port():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('127.0.0.1', 0))
port = sock.getsockname()[1]
sock.close()
return port
@allanlei
allanlei / shell.py
Created January 15, 2014 08:01
Activate Django shell in AWS EB
# Activate the virtualenv
source /opt/python/run/venv/bin/activate
# Then either:
django-admin.py shell --settings=MYAPP.settings
# or if you have a manage.py file in the top level directory of your app:
cd /opt/python/current/app
python manage.py shell
@allanlei
allanlei / diff.py
Created April 14, 2014 06:21
dict diff
# -*- coding: utf-8 -*-
MISSING_KEY_VALUE = None
def diff(d2, d1={}, missing=MISSING_KEY_VALUE):
for key, val in d2.items():
if key not in d1:
yield key, (missing, val)
@allanlei
allanlei / dh.py
Created April 24, 2014 15:28
Diffie Hellman
#!/usr/bin/env python
from binascii import hexlify
import hashlib
# If a secure random number generator is unavailable, exit with an error.
try:
import Crypto.Random.random
secure_random = Crypto.Random.random.getrandbits
except ImportError:
@allanlei
allanlei / gist:300602157a54153b754b
Last active August 29, 2015 14:01
docker + etcd - Command to read all keys from a directory of etcd and set as env in a docker run command
docker run $(etcdctl ls $ETCD_ENV | while read e; do etcdctl get $e > /dev/null 2>&1 && echo --env $(echo $e | rev | cut -d/ -f1 | rev)=$(etcdctl get $e); done) busybox env
@allanlei
allanlei / python-site
Created May 26, 2014 04:53
Sample python backend for nginx
server {
listen 80 default_server deferred;
listen 443 default_server deferred ssl;
listen [::]:80 ipv6only=on default_server deferred;
listen [::]:443 ipv6only=on default_server deferred ssl;
server_name example.com www.example.com testing.example.com;
root /path/to/static/files
# Include SSL stuff
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import urlparse
import click
@click.command()
@click.argument('iam_role')
@click.option('--metadata-url', default='http://169.254.169.254/latest/meta-data/iam/security-credentials/')
@allanlei
allanlei / Dockerfile
Created June 10, 2014 10:42
Command line boto docker container
FROM dockerfile/python
RUN pip install boto
import StringIO
import subprocess
import os
import time
from datetime import datetime
from PIL import Image
# Motion detection settings:
# Threshold (how much a pixel has to change by to be marked as "changed")
# Sensitivity (how many changed pixels before capturing an image)