Skip to content

Instantly share code, notes, and snippets.

@joshmarshall
joshmarshall / tornado_temp_file_put.py
Created March 15, 2011 02:35
Temporary File PUT Transfer Handler for Tornado
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.httputil
import tempfile
class MainHandler(tornado.web.RequestHandler):
def put(self):
filename = self.request.body.name
# do stuff with the image
@joshmarshall
joshmarshall / tornado_temp_json_post.py
Created March 15, 2011 02:43
JSON to Arguments POST in Tornado
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.httputil
import json
class MainHandler(tornado.web.RequestHandler):
def post(self):
# do something useful
name = self.get_argument('foo')
@joshmarshall
joshmarshall / tornado_example.py
Created May 1, 2011 18:34
Tornado RPC Example
from tornadorpc.xml import XMLRPCHandler
from tornadorpc import private, start_server
class Handler(XMLRPCHandler):
def add(self, x, y):
return x+y
def ping(self, obj):
return obj
from httplib import HTTP, HTTPConnection
from urlparse import urlparse
def get_page(url):
parsed = urlparse(url)
conn = HTTPConnection('%s' % parsed[1])
conn.request("GET", parsed[2])
response = conn.getresponse()
data = response.read()
return data
@joshmarshall
joshmarshall / jsonrpclib_console_example.py
Created May 1, 2011 18:50
JSONRPClib Console Example
>>> import jsonrpclib
>>> server = jsonrpclib.Server('http://localhost:8181')
>>> server.add(5, 6)
11
>>> server._notify.add(5, 6)
>>> batch = jsonrpclib.MultiCall(server)
>>> batch.add(10, 17)
>>> batch.ping({'key':'value'})
>>> batch._notify.add(50, 40)
>>> batch()
@joshmarshall
joshmarshall / tornado_xmlrpc_early_test.py
Created May 1, 2011 18:57
Tornado XML-RPC Early Test Script
import tornado.httpserver
import tornado.ioloop
import tornado.web
def private(func):
# Decorator to make a method, well, private.
class PrivateMethod(object):
def __init__(self):
self.private = True
__call__ = func
@joshmarshall
joshmarshall / mouseporter.py
Created June 18, 2011 18:37
A script for starting an x2x+SSH session
#!/usr/bin/env python
"""
AUTHOR: Josh Marshall
ACKNOWLEDGMENTS: Corey(?) http://www.ctrlv.ca/2010/04/163/
REQUIREMENTS: Python 2.6+, x2x, openssh-server
This is just a simple script to start an x2x session over SSH
on a remote machine. It takes a few parameters (like what side of
the screen do you want to use to "cross over" to the other machine,
what SSH port / identify file to use, etc.
@joshmarshall
joshmarshall / gist:1033401
Created June 18, 2011 18:59
A session using mouseporter.py
user@localbox:~$ sudo apt-get install x2x
# On the remote machine...
user@remotebox:~$ sudo apt-get install x2x openssh-server
# Download the code from https://gist.github.com/1033384
# and save it to ~/bin on the local machine
user@localbox:~$ python bin/mouseporter.py 192.168.1.10
Session started... (type Ctrl-C to quit)
# ... and scroll to your right! :)
@joshmarshall
joshmarshall / ichooseyou.py
Created July 29, 2011 02:13
Twitter Winner Chooser
""" This proves that there was no money under the table. """
import random
import sys
from collections import Counter
def main():
""" Pick one of the sys args """
assert len(sys.argv) > 1
names = sys.argv[1:]
@joshmarshall
joshmarshall / euler18.py
Created September 8, 2011 03:44 — forked from ramhiser/gist:1202533
Maximum Binary Tree Sum - Project Euler #18
from numpy import array
raw_data = """75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92