Skip to content

Instantly share code, notes, and snippets.

@ofen
ofen / dns_to_ip.py
Last active April 1, 2020 15:46
Simple asyncio socket DNS to IP resolver
import socket
import asyncio
from urllib.parse import urlsplit
async def get(url):
url = urlsplit(url)
hostname = url.hostname
if url.scheme == 'https':
port = url.port or 443
@ofen
ofen / bottle_cache.py
Last active December 28, 2021 13:20
Simple cache middleware for Bottle
from bottle import route, run, response, request
import datetime
import json
import requests
# Cache middleware
def cache(callback, ttl=datetime.timedelta(hours=1)):
cache = {}
def wrapper(*args, **kwargs):
key = request.path
@ofen
ofen / aio_sheets.py
Created January 17, 2019 20:00
Basic async Google Sheet api request based on aiohttp and asyncio
# Require PyJwt, AIOHTTP and PyCrypto (for RS256 in JWT)
import jwt
import time
import json
from pprint import pprint
import aiohttp
import asyncio