Skip to content

Instantly share code, notes, and snippets.

View josiahcarlson's full-sized avatar

Josiah Carlson josiahcarlson

View GitHub Profile
@josiahcarlson
josiahcarlson / make-key.sh
Created October 29, 2011 01:49
Create an OpenVPN key and zip it up with encryption for sending to your users.
#!/bin/bash
# Toss me in your /easy-rsa/2.0 path to have a quick and easy way to generat
# a key for a new machine, automatically zipped with encryption into /tmp.
# Requires that you have the below lines uncommented as part of a pre-configured
# client.conf sitting in keys/ .
# cert CHANGEME.crt
# key CHANGEME.key
source ./vars
@josiahcarlson
josiahcarlson / async_http_client.py
Created December 25, 2011 23:24
Simple asyncore/asynchat derived http client
'''async_http_client.py
Originally written December 25, 2011 by Josiah Carlson
Released into the public domain.
This simple asynchat.async_chat subclass offers the ability to connect to http
servers to download files. The example download_file() function shows how to
use the class to download files from a remote webserver, automatically
handling redirects and errors.
@josiahcarlson
josiahcarlson / class_property.py
Created January 4, 2012 19:20
Get properties... on classes!
'''
Written January 4, 2012 by Josiah Carlson
Released into the public domain.
I've only ever needed this once, but I had to learn the descriptor protocol.
Works just like a property, except that what you decorate gets the class
instead of the instance.
class Example(object):
@josiahcarlson
josiahcarlson / convert_agile_author_annotations.py
Created February 26, 2012 01:46
Convert Word-style annotations to Agile Author style annotations for Manning books
'''convert_agile_author_annotations.py
Written February 25, 2012 by Josiah Carlson
Released into the public domain.
Did you write your annotions for Agile Author using the Word style?
<example id="...">
<title>title</title>
<programlisting>
@josiahcarlson
josiahcarlson / goto.py
Created April 1, 2012 17:04
Decorators for adding goto/switch semantics to Python
'''
This code is derived from: http://code.activestate.com/recipes/576944/
It was released under the MIT license.
I (Josiah Carlson) have modified it to be correct in more cases, and to report
errors in some error cases.
@josiahcarlson
josiahcarlson / redis_sentinel.py
Created May 12, 2012 20:28
Redis sentinel monitor assignments
# A proposed method for choosing which slaves to monitor within the
# Redis HA sentinel monitors
def select_slaves_to_monitor(slave_info, all_sentinels, my_id, my_slaves):
'''
slave_info = [
('ip_or_host:port', ['sentinel-id, ...]),
...
]
@josiahcarlson
josiahcarlson / chunked_server_test.py
Last active March 20, 2024 20:30
Use some standard Python libraries to implement a chunked-transfer encoding web server with partially-working gzip support
'''
chunked_server_test.py
Copyright August 3, 2012
Released into the public domain
This implements a chunked server using Python threads and the built-in
BaseHTTPServer module. Enable gzip compression at your own peril - web
browsers seem to have issues, though wget, curl, Python's urllib2, my own
async_http library, and other command-line tools have no problems.
@josiahcarlson
josiahcarlson / pass_socket.py
Created September 14, 2012 18:05
An example of passing a socket between processes using Python's multiprocessing library
'''pass_socket.py
Written September 14, 2012
Released into the public domain.
Works on Python 2.6, 2.7, and may need minor changes for 3+.
'''
import multiprocessing
@josiahcarlson
josiahcarlson / refresh_lock.py
Created October 15, 2012 05:36
A way to refresh a lock from chapter 6 of Redis in Action
'''A function to refresh a lock that can timeout, before it times out.'''
def refresh_lock(conn, lockname, identifier, lock_timeout=10):
pipe = conn.pipeline(True)
lockname = 'lock:' + lockname
while True:
try:
pipe.watch(lockname)
if pipe.get(lockname) == identifier:
@josiahcarlson
josiahcarlson / parallel_mget.py
Created December 7, 2012 15:26
A function to do parallel pipelined mget calls to a group of shards
'''
Written December 7, 2012 by Josiah Carlson
Released into the public domain.
Untested.
'''
from binascii import crc32