Skip to content

Instantly share code, notes, and snippets.

View simonw's full-sized avatar

Simon Willison simonw

View GitHub Profile
/* For Node.js - deal with some of the boilerplate involved in HTTP GET requests */
var http = require('http'),
urlmod = require('url');
function getResponse(url, callback) {
var parsed = urlmod.parse(url);
var port = parseInt(parsed.port || '80', 10);
var client = http.createClient(port, parsed.hostname);
var path = parsed.pathname;
/* For Node.js - resolve a chain of HTTP redirects
Uses getResponse() from http://gist.github.com/399276
Example: resolveHttpRedirects('http://ow.ly/1Kn4j', function(url) { sys.puts(url) });
*/
function resolveHttpRedirects(url, callback, maxnum) {
maxnum = maxnum || 3;
var count = 0;
function next(url) {
getResponse(url, function(response) {
import django
from django.template import Template, Context
template_string = """
The answer to the Ultimate
Question is {{ everything.universe.answer }}.
Among our weapons are:
{% for weapon in weapons %}
{{ weapon }}
/* Multiget web service, written in Node
http://127.0.0.1:9001/?urls=http://example.com/,http://example.net/
Will fetch all URLs in parallel, then return a JSON response containing
the fetched content from them.
*/
var DEFAULT_TIMEOUT = 10000;
/* Evil script for exporting LinkedIn contacts along with their locations.
Navigate to http://www.linkedin.com/connections?trk=hb_tab_cnts and
click on the "Locations" accordion button, then paste the following
script in to the Google Chrome JavaScript console (untested in anything
else). After quite a while a popup with some JSON will appear. */
function fakeClick(el) {
var e = document.createEvent('MouseEvents');
e.initEvent('click', true, true);
el.dispatchEvent(e);
@simonw
simonw / http_get_headers_and_truncated_body.py
Created August 30, 2010 12:58
Do a GET against a URL, timing out sensibly, following up to a max number of redirects and pulling down only the first X bytes of the body
import httplib, urlparse, socket
def http_get_headers_and_truncated_body(
url, max_redirects=5, body_length=2048, timeout=5,
allowed_content_types = ('text/html',), num_redirects_followed=0,
redirect_chain = None
):
redirect_chain = redirect_chain or []
# Returns {'ok':True,'headers':{},'redirected':False,'url':'','body': '',
# 'max_redirects_reached': False, 'num_redirects_followed': 0,
@simonw
simonw / installing-redis-2.0-on-ubuntu.txt
Created August 30, 2010 14:25
Installing Redis 2.0.0 on an Ubuntu server
wget http://mirrors.kernel.org/ubuntu/pool/universe/r/redis/redis-server_2.0.0~rc2-1_i386.deb
sudo dpkg -i redis-server_2.0.0~rc2-1_i386.deb
"""
Here's how to run a command from Python piping a string in to stdin and reading the
output back in to another string. It took me way too long to work this out.
"""
from subprocess import Popen, PIPE
output = Popen(
['java', '-jar', 'yuicompressor-2.4.2.jar', '--type=css'],
stdin=PIPE, stdout=PIPE
/* Inspired by Python's useful re.findall method */
function findall(regex, str) {
var matches = [], m;
regex.lastIndex = 0;
while (m = regex.exec(str, regex.lastIndex)) {
matches[matches.length] = m;
}
return matches;
}