Skip to content

Instantly share code, notes, and snippets.

@stuntgoat
stuntgoat / gist:1394818
Created November 26, 2011 01:48
NSURLConnection Delegate
// inside MyHTTPRequest.m
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {
/* This delegate method is called before the connection sends a NSURLRequest.
(NSURLResponse *)response will be nil when this method is not called by the delegate recieving a redirect
(NSURLRequest *)request will contain the NSURLRequest object that (NSURLConnection *)connection is about
to send to the server.
For example, during an initial request to a server, this delegate method is called
and (NSURLResponse *)response will be nil, since the delegate has not received a HTTP
redirect ( the delegate has not received any response from the server yet because no request
@stuntgoat
stuntgoat / gist:1394827
Created November 26, 2011 01:59
Portion of an example as demonstrated in using NSURLConnection Delegate
// MyHTTPRequest.h
@interface MyHttpRequest : NSObject {
// The following 3 class attributes are saved as the initial request is created.
// If a redirect occurs, these attributes will be used to copy to the last redirected request,
// only if the URL is the same as the initial request's URL
NSData *requestBodyData;
NSString *requestMethod; // POST or GET
NSURL *requestURL; // initial request's URL
// other attributes omitted
}
@stuntgoat
stuntgoat / gist:1394843
Created November 26, 2011 02:15
@login_required decorator example portion
# if the client request does not contain a valid session id, the
# client will receive a redirect to http://10.0.0.2/signin/
@login_required(login_url='/signin/')
def user_home_view(request):
# . . . returns the user's home view
@stuntgoat
stuntgoat / gist:1394856
Created November 26, 2011 02:36
Login view that sends request for credentials to the client via WWW-Authenticate
def auth_landing_page(request):
try:
if request.META['HTTP_AUTHORIZATION']:
# in httpd.conf set 'WSGIPassAuthorization On'
# WSGIPassAuthorization will pass the login information sent by the client to the Django view,
# as shown below.
http_authorization = request.META['HTTP_AUTHORIZATION']
import base64
_user, _password = base64.b64decode(http_authorization[6:]).split(':')
user = authenticate(username=_user, password=_password)
@stuntgoat
stuntgoat / encoder.py
Created December 13, 2011 06:42
Added pretty printing in HTML for json.encoder.py in Python 2.7.1
"""Implementation of JSONEncoder
Added pretty printing to html, wherein indents are "&nbsp;" and newlines are "<br>".
# added html keyword arg; default is html=False
json.dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, html[, separators[, encoding[, default[, **kw]]]]]]]]]]])
Serialize obj to a JSON formatted str.
If html is True and indent is not None the encoder indents are printed as "&nbsp;" and newlines are "<br>"
"""
import re
@stuntgoat
stuntgoat / ws.py
Created April 3, 2012 19:52
websocket handling for Brubeck
# I am using http://isr.nu/ws/WebSocketTest.htm to help debug my websocket connection
# and I send websocket requests to ws://127.0.0.1:6767/websockets
######## IMPORTANT:
######## Add the websocket method to the MessageHandler class
## inside brubeck.request_handling- add 'websocket' method:
# HTTP_METHODS = ['get', 'post', 'put', 'delete',
# 'head', 'options', 'trace', 'connect', 'websocket']
@stuntgoat
stuntgoat / mongrel2.error.log
Created April 3, 2012 20:06
mongrel2 timeout
Tue, 03 Apr 2012 20:04:50 GMT [WARN] (src/register.c:336: errno: Resource temporarily unavailable) Killed 1 connections according to min_ping: 120, min_write_rate: 300, min_read_rate: 300
Tue, 03 Apr 2012 20:04:50 GMT [WARN] (src/mongrel2.c:205: errno: Resource temporarily unavailable) Timeout task killed 1 tasks, waiting 10 seconds for more.
@stuntgoat
stuntgoat / osc-recv.ck
Created April 21, 2012 20:55
OSC receive dot ChucK
// (launch with OSC_send.ck)
// the patch
// create our OSC receiver
OscRecv recv;
// use port 6449
6449 => recv.port;
// start listening (launch thread)
recv.listen();
@stuntgoat
stuntgoat / tests.txt
Created May 13, 2012 05:44
zlib and Redis tests with Python
setup_compress = """
for i in xrange(10000):
r.hset(str(i), str(i), zlib.compress(10 * "my name is mud", 1))"""
setup_normal = """
for i in xrange(10000):
r.hset(str(i), str(i), 10 * "my name is mud")"""
@stuntgoat
stuntgoat / websocket_demo2.py
Created May 16, 2012 01:36
websocket demo to send multiple messages in Brubeck
import sys
import logging
import os
from brubeck.request_handling import Brubeck, WebMessageHandler
from brubeck.connections import Mongrel2Connection
from ws4py.framing import Frame, \
OPCODE_CONTINUATION, OPCODE_TEXT, \
OPCODE_BINARY, OPCODE_CLOSE, OPCODE_PING, OPCODE_PONG