Skip to content

Instantly share code, notes, and snippets.

View tarekziade's full-sized avatar

Tarek Ziade tarekziade

View GitHub Profile
@tarekziade
tarekziade / grum.py
Last active April 5, 2017 14:24
poutine test
import asyncio
import configparser
import pytest
from smwogger import API
@pytest.fixture
def conf():
config = configparser.ConfigParser()
config.read('manifest.ini')
@tarekziade
tarekziade / call_hb.py
Created April 4, 2017 14:00
Smowgger Example
import asyncio
from smwogger import API
async def run_heartbeat():
async with API('__api__.json') as api:
res = await api.__heartbeat__()
body = await res.json()
print(body)
@tarekziade
tarekziade / check_redirect.py
Created March 18, 2017 09:06
White list any 302 locations
from flask import make_response
from urllib.parse import urlparse
# domain:port
SAFE_DOMAINS = ['ziade.org:443']
@app.after_request
def check_redirect(response):
if response.status_code != 302:
@tarekziade
tarekziade / api.py
Last active August 1, 2018 16:57
connexion as an extension
import pathlib
from connexion.api import Api, Resolver
import werkzeug.exceptions
def swagger_blueprint(specification, specification_dir=None, validate_responses=False,
strict_validation=False, resolver_error=None,
base_path='', debug=False):
resolver = Resolver()
swagger_json = False
@tarekziade
tarekziade / async_workers.py
Created January 9, 2017 21:03
Async Workers using Threads
import requests
import asyncio
import sys
from functools import partial
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
loop = asyncio.get_event_loop()
@tarekziade
tarekziade / detect.rst
Created October 14, 2016 14:02
Detect and kill an idling loads ec2 instance

On the EC2 instance A few variable to set:

Run a docker with the docker sock linked and a few variables set:

docker run -t -v /var/run/docker.sock:/run/docker.sock -v $(which docker):/bin/docker  -i ubuntu /bin/bash
apt-get update && apt-get install -y python-pip && pip install docker-py==1.6.0

export CID=`cat /proc/self/cgroup | grep -o  -e "docker-.*.scope" | head -n 1 | sed "s/docker-\(.*\).scope/\\1/"`
@tarekziade
tarekziade / distribution.py
Last active June 4, 2016 05:39
Consistent Distribution of users across servers
""" Consistent load-balancing.
We have a few servers and we want a load-balancer to
distribute incoming requests across them in a deterministic
and consistent way - without keeping any counter to make the
decision.
Removing a backend server should not impact users on other
servers.
import os
from multiprocessing.pool import Pool, TimeoutError
import signal
import functools
import time
class TimeoutFunc(object):
def __init__(self, func, duration, ctimeout):
self.func = func
@tarekziade
tarekziade / verify.py
Created November 19, 2012 14:23
Verifying a gpg signed package from PyPI
# you need to install python-gpgme
import StringIO
import urllib
import gpgme
import shutil, tempfile
import os
from contextlib import closing, contextmanager
tarball = 'http://pypi.python.org/packages/source/s/signtest/signtest-1.0.tar.gz'
@tarekziade
tarekziade / hard_error.rst
Created October 17, 2012 20:09
Hard to find error in Python 2

So you have some data mapping you format in a string:

>>> data = {'a': 'é', 'b': 's'}
>>> '%(b)s %(a)s' % data
's \xc3\xa9'

Nice. Python is simply formating the string and everything works.

Now what happens if one of the keys is unicode: