Skip to content

Instantly share code, notes, and snippets.

@0xKD
0xKD / drf_mixin_related.py
Last active October 20, 2016 15:46
Django Rest Framework - Setting related field
@0xKD
0xKD / async.py
Created September 15, 2016 16:46
import aiohttp
import asyncio
import time
@asyncio.coroutine
def get_data(client):
with aiohttp.Timeout(10):
response = yield from client.get('https://www.google.co.in')
return (yield from response.text())
@0xKD
0xKD / share_term.sh
Last active July 15, 2016 11:33
Share terminal over network
# start named session in tmux
tmux new-session -s my-session
# (in another term) start gotty
gotty -r --random-url-length "24" tmux attach -t my-session # gotty -w if you need clients to be able to write (danger!)
# (in another term) to share over the Internet.
ngrok http 8080
# Otherwise setup firewall, portforward, and share public IP. (better perf than ngrok)
@0xKD
0xKD / dbconnect.py
Created July 15, 2016 08:41
Discover arbitrary db using SQLAlchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, MetaData
from collections import namedtuple
import hashlib
import os
import pickle
@0xKD
0xKD / diagonals_sum.py
Created March 9, 2016 16:17
Sum of Diagonals
"""
73 74 75 76 77 78 79 80 81
72 43 44 45 46 47 48 49 50
71 42 21 22 23 24 25 26 51
70 41 20 7 8 9 10 27 52
69 40 19 6 1 2 11 28 53
68 39 18 5 4 3 12 29 54
67 38 17 16 15 14 13 30 55
66 37 36 35 34 33 32 31 56
@0xKD
0xKD / hn-comments.js
Created February 12, 2016 11:11
Collapse HN comments (WIP)
(function() {
'use strict';
function get_comment_index(seed, currWidth, prevWidth, prevIndex) {
if (currWidth === 0 || prevIndex === undefined) {
return seed;
}
if (currWidth > prevWidth) {
return prevIndex + '-' + (seed + 1);
} if (currWidth == prevWidth) {
@0xKD
0xKD / xhrpost.js
Last active January 25, 2016 09:59
Make HTTP POST using XMLHttpRequest
var xhr = new XMLHttpRequest();
url = '/url/to/post';
params = JSON.stringify({'key': 'value'});
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Content-Length", params.length);
xhr.setRequestHeader("Connection", "close");
xhr.onreadystatechange = function() {
@0xKD
0xKD / index.html
Created December 4, 2015 02:22
Realtime (rethinkdb changefeeds, socket.io)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Loclog</title>
<style>
body, html {
padding: 0;
}
@0xKD
0xKD / parse.py
Last active June 26, 2016 11:11
Clone webpage for local browsing
#!/usr/local/bin/python3
from urllib.parse import urlparse
import sys
if __name__ == '__main__':
if len(sys.argv) == 2:
sys.stdout.write(urlparse(sys.argv[1]).netloc)
else:
sys.stdout.write('doesnotexist.lampost')
@0xKD
0xKD / heap.py
Created October 17, 2015 14:30
Is this a heap?
class Heap:
"""Heap implementation"""
def __init__(self, arr):
self.arr = arr
def parent(self, child_index):
return self.arr[child_index // 2]
def left(self, parent_index):