Skip to content

Instantly share code, notes, and snippets.

View abersheeran's full-sized avatar
👋
玉楼金阙慵归去,且插梅花醉洛阳

Aber abersheeran

👋
玉楼金阙慵归去,且插梅花醉洛阳
View GitHub Profile
@abersheeran
abersheeran / headers.py
Last active June 14, 2020 15:26
get fake User-Agent in desktop or moblie
import random
DESKTOP_USER_AGENT = [
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
"Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
"Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
@abersheeran
abersheeran / get-cn-ipv4.sh
Last active March 22, 2024 06:38
Chinese IPv4
wget -c http://ftp.apnic.net/stats/apnic/delegated-apnic-latest
cat delegated-apnic-latest | awk -F '|' '/CN/&&/ipv4/ {print $4 "/" 32-log($5)/log(2)}' | cat > ip.txt
@abersheeran
abersheeran / init.wsl
Created June 27, 2020 20:09
Auto run wsl script on Windows10 startup
/etc/init.d/supervisor $1
@abersheeran
abersheeran / bloomfilter.py
Last active March 25, 2022 06:00
Bloom filter by python3
import math
from hashlib import blake2b
class BloomFilter:
def __init__(self, elements_count: int, *, error_rate: float = 0.0001) -> None:
byte_count = 1 + int(
-(math.log(error_rate) / math.log(2) ** 2) * elements_count // 8
)
digest_size = int(math.log2(elements_count)) + (elements_count % 2)
@abersheeran
abersheeran / EnableLoopback.bat
Created December 31, 2020 11:13
Allow all UWP applications access 127.0.0.1 proxy
FOR /F "tokens=11 delims=\" %p IN ('REG QUERY "HKCU\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppContainer\Mappings"') DO CheckNetIsolation.exe LoopbackExempt -a -p=%p
@abersheeran
abersheeran / ensure_pep563.py
Last active February 3, 2023 10:27
Ensure that all Python modules in the specified directory are turned on pep563
from pathlib import Path
def ensure_pep563(dirpath: Path) -> None:
self_path = Path(__file__).absolute()
future_import = "from __future__ import annotations"
for pypath in dirpath.glob("**/*.py"):
if pypath.absolute() == self_path:
continue
@abersheeran
abersheeran / wsgi-type-definitions.py
Last active November 20, 2023 04:48
use typing to describe WSGI
"""
https://peps.python.org/pep-3333/
"""
from types import TracebackType
from typing import (
Any,
Callable,
Iterable,
List,
Optional,
@abersheeran
abersheeran / y.py
Created April 6, 2021 03:11
Y combinator. Python lambda.
Y = lambda g: (lambda f: g(lambda arg: f(f)(arg))) (lambda f: g(lambda arg: f(f)(arg)))
if __name__ == "__main__":
fib = Y(lambda f: lambda n: (1 if n < 3 else f(n-1) + f(n-2)))
print(fib(10))
from typing import Any, Dict, List
from django.core.exceptions import FieldDoesNotExist
from django.db import models
def serialize_model(model: models.Model) -> Dict[str, Any]:
result = {
name: serialize_model(foreign_key)
for name, foreign_key in model.__dict__["_state"].__dict__.get("fields_cache", {}).items()
@abersheeran
abersheeran / wsl-docker.sh
Last active September 7, 2021 06:19
Allow use docker commands in WSL1
#!/bin/bash
sudo apt-get update
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release