Skip to content

Instantly share code, notes, and snippets.

@sammachin
sammachin / ws_socket_server.py
Last active August 29, 2015 14:26
Callbacks inside WebSocket Connections
#!/usr/bin/env python
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
from datetime import datetime
import sched, time
class WSHandler(tornado.websocket.WebSocketHandler):
@sammachin
sammachin / validate.py
Created May 2, 2015 10:03
Twilio Request Validation in Tornado on Heroku
import os
import tornado.httpserver
import tornado.ioloop
import tornado.web
from twilio import twiml
from oauth2client.client import SignedJwtAssertionCredentials
from twilio.util import RequestValidator
class ValidateHandler(tornado.web.RequestHandler):
@sammachin
sammachin / validate.py
Created May 1, 2015 20:05
Tornado Validate Twilio Request
class Validate(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
signature = self.request.headers.get('X-Twilio-Signature')
AUTH_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
validator = RequestValidator(AUTH_TOKEN)
url = self.request.uri
var = self.request.arguments
for x in var:
var[x] = ''.join(var[x])
@sammachin
sammachin / conv.pl
Created February 5, 2015 13:26
convert BE/LE binary now with MOAR perl
my($input) = 0b01000000010000100000111100000000;
my $v1 = pack("I>", $input);
my $v2 = unpack("I<", $v1);
print "$v2"
@sammachin
sammachin / gist:6c2b91d52b1052b0ec58
Created February 5, 2015 13:00
Convert 32bit binary int from BE to LE to Dec.
import struct
def convert(input):
v1 = struct.pack('>I', int(input, 2))
v2 = struct.unpack('<I', v1)
output = int(''.join(map(str,v2)))
return output
@sammachin
sammachin / gist:3bd36c1957787206d060
Last active August 29, 2015 14:11
Excerpt from my asterisk extenstions.conf to screen my incomming calls and ask unknown's to press 1 before it will call me
[from-external]
// Send Notification
exten => 01934xxxxxx,1,AGI(agi://192.168.1.11:4573/log?chan=landline&cli=${CALLERID(num)})
// Allow whitelisted numbers
exten => 01934xxxxxx/07970xxxxxx,2,Dial(SIP/1000&SIP/1001&SIP/1002&SIP/2000)
exten => 01934xxxxxx/01202xxxxxx,2,Dial(SIP/1000&SIP/1001&SIP/1002&SIP/2000)
@sammachin
sammachin / config.xml
Created November 23, 2014 14:54
Firebrick Twilio Config
<rule-set no-match-action="continue">
<rule target-port="5060"
action="accept"/>
</rule-set>
<rule-set name="Block Incomming"
source-interface="pppoe"
target-interface="LAN"
no-match-action="reject"
comment="Default firewall rule - block incoming">
### Keybase proof
I hereby claim:
* I am sammachin on github.
* I am sammachin (https://keybase.io/sammachin) on keybase.
* I have a public key whose fingerprint is 4867 9F57 F2C9 7E38 62D2 5838 890C 49E3 52CD 329B
To claim this, I am signing this object:
@sammachin
sammachin / test.r
Created August 20, 2014 14:07
rjstat problems
# Using v0.1 copy from our GitHub repo
install_github("rjstat", "ONSDigital")
url <- "http://s3.sammachin.com/QS103EW.json"
results <- fromJSONstat(readLines(url))
length(results$`Age by single year`$value)
[1] 102
@sammachin
sammachin / ons-api.py
Created August 7, 2014 11:37
Fetch Data from the ONS-API as JSON STAT then turn that into a set of simple numbers
#! /usr/bin/env python
import json
import requests
def process(obj, ds):
data = {}
values = obj[ds]['value']
index = obj[ds]['dimension'][obj[ds]['dimension']['id'][1]]['category']['index']
labels = obj[ds]['dimension'][obj[ds]['dimension']['id'][1]]['category']['label']