Skip to content

Instantly share code, notes, and snippets.

@laixintao
laixintao / decent_request.py
Last active January 18, 2025 08:12
Send HTTP requests using python-requests with timeout, tcp reuse(session) and retry.
from requests.adapters import HTTPAdapter, Retry
from requests import Session
retries = Retry(
total=5, backoff_factor=1, status_forcelist=[502, 503, 504]
)
session = Session() # reuse tcp connection
session.mount("http://", HTTPAdapter(max_retries=retries))
session.mount("https://", HTTPAdapter(max_retries=retries))
@fanliugen
fanliugen / Python Tornado Queue.py
Created December 20, 2019 08:10 — forked from Integralist/Python Tornado Queue.py
[Python Tornado Queue] #python #tornado #queue
import logging
import time
import tornado.gen
import tornado.ioloop
import tornado.queues
import tornado.web
class Client():
@PatrikHlobil
PatrikHlobil / nested_iterator.py
Last active July 10, 2025 00:28
Get all keys or values of a nested dictionary or list in Python
def iterate_all(iterable, returned="key"):
"""Returns an iterator that returns all keys or values
of a (nested) iterable.
Arguments:
- iterable: <list> or <dictionary>
- returned: <string> "key" or "value"
Returns:
@hduffddybz
hduffddybz / client.py
Last active September 13, 2024 01:29
Python Select Server & Client
#!/usr/bin/python
import socket, select
host = '121.40.77.208'
port = 10000
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect((host, port))
inout = [socket]
@simonw
simonw / gist:7000493
Created October 15, 2013 23:53
How to use custom Python JSON serializers and deserializers to automatically roundtrip complex types.
import json, datetime
class RoundTripEncoder(json.JSONEncoder):
DATE_FORMAT = "%Y-%m-%d"
TIME_FORMAT = "%H:%M:%S"
def default(self, obj):
if isinstance(obj, datetime.datetime):
return {
"_type": "datetime",
"value": obj.strftime("%s %s" % (
@tanbro
tanbro / attrdict.py
Last active January 17, 2024 14:51
A class with both dictionary and attribute style access to it's data member.
from __future__ import annotations
from typing import Any, Dict, Iterable, Mapping, MutableMapping, MutableSequence, Union
class AttrDict:
"""
A class with both dictionary and attribute style access to it's data member.
It can be used in `RestrictedPython <http://restrictedpython.readthedocs.io/>`_