Skip to content

Instantly share code, notes, and snippets.

@RainJayTsai
RainJayTsai / server.py
Created January 4, 2019 08:10 — forked from scturtle/server.py
python socks5 proxy server with asyncio (async/await)
#!/usr/bin/env python3.5
import socket
import asyncio
from struct import pack, unpack
class Client(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
self.server_transport = None
@RainJayTsai
RainJayTsai / http_echo_and_proxy_server.py
Created January 4, 2019 08:10 — forked from pomack/http_echo_and_proxy_server.py
A simple python HTTP server that either serves as a proxy or as an echo server. If working as an echo server, it outputs the headers and body into log output and to the client, which is useful when debugging a reverse proxy or caching server to see what is being sent. If working as a proxy server, it outputs what the request headers will be to t…
#!/usr/bin/env python
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import StringIO
import argparse
import logging
import os
import random
import sys
import urllib2
@RainJayTsai
RainJayTsai / coll_counts.py
Created December 7, 2018 02:56
fast_read_big_mongo_collection_doc_counts
from pymongo import MongoClient
client = MongoClient('IP')
db = client['DB']
collection_name = 'your_full_collectaion_name'
resp = db.command('collstats',collection_name)
print(resp.get('count')
@RainJayTsai
RainJayTsai / mongo_.py
Created December 7, 2018 02:50
fast dump mongo collection to memory, using ProcessPoolExecutor multiprocess
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import bson
from typing import Callable, Optional
import concurrent.futures as cf
from pymongo.collection import Collection
def decode(data: bytes, callback: Callable[[list], None]):
@RainJayTsai
RainJayTsai / _aiojob.py
Created October 22, 2018 08:16
python aiojob wrapper for sanic
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from aiojobs import create_scheduler
def get_scheduler_from_app(app):
return app.AIOJOBS_SCHEDULER
def get_scheduler(request):
@RainJayTsai
RainJayTsai / async_worker_pool.py
Created August 22, 2018 06:40 — forked from thehesiod/async_worker_pool.py
Asynchronous Worker Pool, allows for limiting number of concurrent tasks
import asyncio
from datetime import datetime, timezone
import os
def utc_now():
# utcnow returns a naive datetime, so we have to set the timezone manually <sigh>
return datetime.utcnow().replace(tzinfo=timezone.utc)
class Terminator:
pass
@RainJayTsai
RainJayTsai / download-file.js
Created June 12, 2018 02:36 — forked from javilobo8/download-file.js
Download files with AJAX (axios)
axios({
url: 'http://localhost:5000/static/example.pdf',
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf');
document.body.appendChild(link);
@RainJayTsai
RainJayTsai / async-await.js
Created May 31, 2018 01:29 — forked from wesbos/async-await.js
Simple Async/Await Example
// 🔥 Node 7.6 has async/await! Here is a quick run down on how async/await works
const axios = require('axios'); // promised based requests - like fetch()
function getCoffee() {
return new Promise(resolve => {
setTimeout(() => resolve('☕'), 2000); // it takes 2 seconds to make coffee
});
}
@RainJayTsai
RainJayTsai / async-await.js
Created May 31, 2018 01:29 — forked from wesbos/async-await.js
Simple Async/Await Example
// 🔥 Node 7.6 has async/await! Here is a quick run down on how async/await works
const axios = require('axios'); // promised based requests - like fetch()
function getCoffee() {
return new Promise(resolve => {
setTimeout(() => resolve('☕'), 2000); // it takes 2 seconds to make coffee
});
}
@RainJayTsai
RainJayTsai / docker-cleanup-resources.md
Created March 27, 2018 13:16 — forked from bastman/docker-cleanup-resources.md
docker cleanup guide: containers, images, volumes, networks

Docker - How to cleanup (unused) resources

Once in a while, you may need to cleanup resources (containers, volumes, images, networks) ...

delete volumes

// see: https://github.com/chadoe/docker-cleanup-volumes

$ docker volume rm $(docker volume ls -qf dangling=true)

$ docker volume ls -qf dangling=true | xargs -r docker volume rm