Skip to content

Instantly share code, notes, and snippets.

@dishwad
dishwad / logger.py
Last active June 19, 2024 04:04
Logging configuration for FastAPI using Gunicorn + Uvicorn workers
import logging
import sys
import time
from typing import Callable
from fastapi import Request, Response
from fastapi.routing import APIRoute
from gunicorn.glogging import Logger
from loguru import logger
from starlette.background import BackgroundTask
@ohforest
ohforest / readme.md
Created February 17, 2023 10:46 — forked from fengyuentau/readme.md
Enable X11 forward for ssh to load images from remote server on MacOS Mojave

Enable X11 forward to load images from remote server on MacOS Mojave

Steps

  1. Install Xquartz to get X11 support on MacOS. You can google Xquartz and download it from its official site, or install using HomeBrew.

    brew cask install xquartz
  2. Launch Xquartz. Go to Preference -> Security, click the box Allow connections from clients. NOTE: You have to lauch Xquartz with Allow connections from clients enable everytime you want to ssh to remote server with X11 forwarding support.

#!/bin/bash
set -e
cf_ips() {
echo "# https://www.cloudflare.com/ips"
echo "geo \$realip_remote_addr \$cloudflare_ip {"
echo "default 0;"
for type in v4 v6; do
@ohforest
ohforest / iptables.sh
Created December 1, 2020 06:07 — forked from thomasfr/iptables.sh
iptable rules to allow outgoing DNS lookups, outgoing icmp (ping) requests, outgoing connections to configured package servers, outgoing connections to all ips on port 22, all incoming connections to port 22, 80 and 443 and everything on localhost
#!/bin/bash
IPT="/sbin/iptables"
# Server IP
SERVER_IP="$(ip addr show eth0 | grep 'inet ' | cut -f2 | awk '{ print $2}')"
# Your DNS servers you use: cat /etc/resolv.conf
DNS_SERVER="8.8.4.4 8.8.8.8"
# Allow connections to this package servers
@Integralist
Integralist / 0. README.md
Last active July 2, 2024 02:56
[Python Context and ContextVars] #python #python3 #context #contextvars

Context variables are variables that can have different values depending on their context. They are similar to Thread-Local Storage in which each execution thread may have a different value for a variable. However, with context variables, there may be several contexts in one execution thread. The main use case for context variables is keeping track of variables in concurrent asynchronous tasks. -- https://realpython.com/python37-new-features/#context-variables

"""Example copied verbatim from Real Python."""

import contextvars

name = contextvars.ContextVar("name")
contexts = list()
@RabeyaMuna
RabeyaMuna / Q4.py
Created May 7, 2020 02:43
The parent_directory function returns the name of the directory that's located just above the current working directory. Remember that '..' is a relative path alias that means "go up to the parent directory". Fill in the gaps to complete this function.
import os
def parent_directory():
# Create a relative path to the parent
# of the current working directory
relative_parent = os.path.join(os.getcwd(), os.pardir)
# Return the absolute path of the parent directory
return os.path.abspath(relative_parent)
print(parent_directory())
@ohforest
ohforest / network-tweak.md
Created March 28, 2020 16:52 — forked from mustafaturan/network-tweak.md
Linux Network Tweak for 2 million web socket connections

Sample config for 2 million web socket connection

    sysctl -w fs.file-max=12000500
    sysctl -w fs.nr_open=20000500
    # Set the maximum number of open file descriptors
    ulimit -n 20000000

    # Set the memory size for TCP with minimum, default and maximum thresholds 
 sysctl -w net.ipv4.tcp_mem='10000000 10000000 10000000'
@unstppbl
unstppbl / go_time_parsing.md
Last active September 23, 2024 07:10
Parsing custom time layout in Golang

There are some key values that the time.Parse is looking for.

By changing:

test, err := time.Parse("10/15/1983", "10/15/1983")

to

@nhtua
nhtua / 00.install-android-sdk.sh
Last active March 26, 2025 10:25
Run a Headless Android Device on Ubuntu server (no GUI)
#!/bin/bash -i
#using shebang with -i to enable interactive mode (auto load .bashrc)
set -e #stop immediately if any error happens
# Install Open SDK
apt update
apt install openjdk-8-jdk -y
update-java-alternatives --set java-1.8.0-openjdk-amd64
java -version