Skip to content

Instantly share code, notes, and snippets.

View wil's full-sized avatar

Wil Tan wil

View GitHub Profile
@wil
wil / echo.c
Created November 12, 2009 15:29 — forked from paulsmith/echo.c
Nothing here, just watching it
/**
* A simple preforking echo server in C.
*
* Building:
*
* $ gcc -Wall -o echo echo.c
*
* Usage:
*
* $ ./echo
#!/usr/bin/env python
#
### patch socket and tornado
import gevent.monkey; gevent.monkey.patch_all()
import gtornado.monkey; gtornado.monkey.patch_all()
import tornado.httpserver
import tornado.ioloop
import tornado.options
@wil
wil / gist:347596
Created March 29, 2010 08:25
Django Setting Expires Header in XHR Response
import time
from django.utils.http import http_date
AJAX_NEGATIVE_CHECK_EXPIRES = 60 # object is still available
AJAX_POSITIVE_CHECK_EXPIRES = 60*10 # if object is not available (or taken)
def check_ajax(request):
# do stuff here
timeout = AJAX_NEGATIVE_CHECK_EXPIRES if avail else AJAX_POSITIVE_CHECK_EXPIRES
@wil
wil / find_forelse.py
Created October 24, 2011 11:49
Look for the for/else construct in python code
#!/usr/bin/env python
# Parses a .py file and look for for-else constructs
# Usage: find_forelse.py some_python_file.py
import sys
import ast
def check_file(filename):
with open(filename, "r") as f:
@wil
wil / django_sql_debug.py
Created January 3, 2012 10:51
Django Production SQL Debugging in the manage.py shell
# These are some commands to copy and paste into a manage.py shell session
# in order to find out what SQL queries are generated by a view
## turn on DEBUG so that ``connection.queries`` gets populated
from django.conf import settings
settings.DEBUG = True
from django.db import connection
## handy function for printing the queries
@wil
wil / fake_django_request
Created January 3, 2012 11:45
Fake django request useful for calling a view function
from django.http import QueryDict
class FakeRequest(object):
pass
request = FakeRequest()
request.GET = QueryDict('a=1&b=2')
request.META = {}
@wil
wil / convert_cache_setting.py
Created October 8, 2012 05:00
Django 1.3 CACHE_BACKEND settings backward compatibility
# This allows you to maintain your Django cache settings in Django 1.3 style i.e. settings.CACHE
# and convert it to the old settings.CACHE_BACKEND url so that you can run it under Django 1.2 or earlier
from django.core.exceptions import ImproperlyConfigured
CACHE_BACKEND_MAP = {
'django.core.cache.backends.db.DatabaseCache': 'db',
'django.core.cache.backends.dummy.DummyCache': 'dummy',
VERSION="0.9.9"
BUILD="betable1"
set -e -x
# Keep track of the original working directory.
OLDESTPWD="$PWD"
# Work in a temporary directory.
cd "$(mktemp -d)"
@wil
wil / install-root-key.sh
Created July 10, 2013 14:17
downloads the public key from the "cloud" environment
#!/bin/sh
#
# install-root-key.sh
#
# This script downloads the public key from the "cloud" environment.
if [ ! -d /root/.ssh ]; then
mkdir -p /root/.ssh
chmod 700 /root/.ssh
fi
@wil
wil / libresolv_query.c
Created August 2, 2013 16:27
Demonstrates how to send a DNS query and parse response in C using libresolv.
/*
* Copyright (c) 2013 by Wil Tan <wil@cloudregistry.net>
*
* Based on dump_dns.c from the dnscap <https://www.dns-oarc.net/tools/dnscap>
* originally written by Paul Vixie.
*
* Copyright (c) 2007 by Internet Systems Consortium, Inc. ("ISC")
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above