Skip to content

Instantly share code, notes, and snippets.

View miadz's full-sized avatar
🏠
Working from home

miad z miadz

🏠
Working from home
View GitHub Profile
@miadz
miadz / pi.py
Created May 28, 2023 16:48 — forked from joshsalako/pi.py
generating the values of pi to a known number of decimal places
def compute_pi(n):
"""
This function calculates the value of pi to 'n' number of decimal places
Args:
n: precision(Decimal places)
Returns:
pi: the value of pi to n-decimal places
"""
decimal.getcontext().prec = n + 3
@miadz
miadz / pi.py
Created May 28, 2023 16:46 — forked from markhamilton1/pi.py
pi is a Python script that computes each digit of the value of pi. As long as this script runs it continues to generate digits. As a matter of full disclosure, I did not write this. I am posting it here as a means of preserving the algorithm and making it available to others.
import sys
def calcPi():
q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
while True:
if 4*q+r-t < n*t:
yield n
nr = 10*(r-n*t)
n = ((10*(3*q+r))//t)-10*n
q *= 10
@miadz
miadz / asyncio_loops.py
Created October 22, 2020 22:16 — forked from lars-tiede/asyncio_loops.py
asyncio + multithreading: one asyncio event loop per thread
import asyncio
import threading
import random
def thr(i):
# we need to create a new loop for the thread, and set it as the 'default'
# loop that will be returned by calls to asyncio.get_event_loop() from this
# thread.
loop = asyncio.new_event_loop()
@miadz
miadz / worker_pool.py
Created October 19, 2020 18:55 — forked from kevinkreiser/worker_pool.py
Work Queue Thread Pool Example in Python
#!/usr/bin/env python
#this is mostly from:
#http://code.activestate.com/recipes/577187-python-thread-pool/
from Queue import Queue
from threading import Thread, Event
from sys import stdout, stderr
from time import sleep
@miadz
miadz / ftplib_simple_proxy.py
Created October 18, 2020 19:33 — forked from fmoo/ftplib_simple_proxy.py
An FTP client implementation that supports sending and receiving data via SOCKS proxy using PASV transfers
import ftplib
import socket
import socks # socksipy (https://github.com/mikedougherty/SocksiPy)
class FTP(ftplib.FTP):
def __init__(self, host='', user='', passwd='', acct='',
timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
proxyconfig=None):
"""Like ftplib.FTP constructor, but with an added `proxyconfig` kwarg
@miadz
miadz / threaded_lbbroker.py
Created October 15, 2020 07:42 — forked from chancyk/threaded_lbbroker.py
PyZMQ Threaded Load-Balancing Broker
"""
Threaded Load-balancing Broker
"""
from __future__ import print_function
from threading import Thread
import zmq
@miadz
miadz / haproxy, sending the source ip to the webserver.
Created October 14, 2020 13:21 — forked from PiBa-NL/haproxy, sending the source ip to the webserver.
haproxy, sending the source ip to the webserver.
To send the ip addres of the client/webbrowser to the server/webserver behind it there are a few options:
1- option forwardfor
2- send-proxy
3- source 0.0.0.0 usesrc clientip
1- option forwardfor
This is an easy option to configure in haproxy, it does require that http layer7 processing is used 'mode http' and the webserver/ webapplication that wants to log or use the ip of the client must use the http-header 'X-Forwarded-For' to read the clientip.
2- send-proxy / send-proxy-v2 / send-proxy-*
This is can be used both with mode tcp and http, it does however require that the server also understands the proxyprotocol. Some applications have added support for this protocol which adds a few bytes with ip information before the actual request.
@miadz
miadz / port-forwarding.py
Created October 3, 2020 02:38 — forked from neoatlantis/port-forwarding.py
port forwarding via python socket
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# TCP Port Forwarding via Socks5 Socket
# Original Author : WangYihang <wangyihanger@gmail.com> (for port forwarding)
# (As gist: <https://gist.github.com/WangYihang/e7d36b744557e4673d2157499f6c6b5e>)
# Changes : NeoAtlantis <aurichalka@gmail.com>
# (adapted to pySocks, argparse for CLI invokation, encryption, etc.)
import argparse
@miadz
miadz / client.py
Created October 1, 2020 16:09
Encrypted Python TCP Socket
import socket
import sys
from tcp import *
def handler(data):
print(data)
return input(">> ")
if __name__ == '__main__':
@miadz
miadz / port-forwarding.py
Created September 30, 2020 14:55
port forwarding via python socket
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# TCP Port Forwarding (Reverse Proxy)
# Author : WangYihang <wangyihanger@gmail.com>
# Requires Python 3.6 or above.
import socket
import threading
import sys