Timeouts for popular Python packages

Overview

Python Timeouts

An unresponsive service can be worse than a down one. It can tie up your entire system if not handled properly. All network requests should have a timeout.

Here’s how to add timeouts for popular Python packages. All have been tested. The default is no timeout, unless otherwise specified. Enjoy!

Build Status

Packages

Standard library

PyPI

Standard Library

http

HTTPConnection(host, port, timeout=1)

Raises socket.timeout

smtplib

SMTP(host, timeout=1)

Raises

  • socket.timeout on connect timeout
  • smtplib.SMTPServerDisconnected on read timeout

socket

sock.settimeout(1)

Raises socket.timeout

subprocess

subprocess.run(cmd, timeout=1)

Raises subprocess.TimeoutExpired

PyPI

aiohttp

timeout = aiohttp.ClientTimeout(total=1)
async with aiohttp.ClientSession(timeout=timeout) as session:
    # ...

Raises asyncio.exceptions.TimeoutError

asyncpg

asyncpg.connect(timeout=1)

Default: 60s

Raises asyncio.exceptions.TimeoutError

boto3

boto3.client('s3', config=Config(connect_timeout=1, read_timeout=1))

Raises

  • botocore.exceptions.ConnectTimeoutError on connect timeout
  • botocore.exceptions.ReadTimeoutError on read timeout

elasticsearch

Elasticsearch(timeout=1)

Raises elasticsearch.exceptions.ConnectionError

mongoengine

connect(connectTimeoutMS=1000, socketTimeoutMS=1000, serverSelectionTimeoutMS=1000)

Raises pymongo.errors.ServerSelectionTimeoutError

mysqlclient

MySQLdb.connect(connect_timeout=1)

Raises MySQLdb._exceptions.OperationalError

opensearch-py

OpenSearch(timeout=1)

Raises opensearchpy.exceptions.ConnectionError

psycopg

psycopg.connect(connect_timeout=1)

Raises psycopg.OperationalError

psycopg2

psycopg2.connect(connect_timeout=1)

Raises psycopg2.OperationalError

pymemcache

Client(host, connect_timeout=1, timeout=1)

Raises socket.timeout

pymongo

MongoClient(connectTimeoutMS=1000, socketTimeoutMS=1000, serverSelectionTimeoutMS=1000)

Default: 20s connect timeout, 30s server selection timeout

Raises pymongo.errors.ServerSelectionTimeoutError

redis

Redis(socket_connect_timeout=1, socket_timeout=1)

Raises redis.exceptions.TimeoutError

requests

requests.get(url, timeout=1)

Raises

  • requests.exceptions.ConnectTimeout on connect timeout
  • requests.exceptions.ReadTimeout on read timeout

SQLAlchemy

create_engine(url, connect_args={'connect_timeout': 1})

Raises sqlalchemy.exc.OperationalError

Don’t see a library you use?

Let us know. Even better, create a pull request for it.

Running the Tests

git clone https://github.com/ankane/python-timeouts.git
cd python-timeouts
pip install -r requirements.txt

To run all tests, use:

pytest

To run individual tests, use:

pytest tests/test_redis.py
Owner
Andrew Kane
Andrew Kane
Building a Robust IOT device which is customizable, encrypted, secure and user friendly

Building a Robust IOT device which is customizable, encrypted, secure and user friendly, which uses a single GPIO pin to extract multiple sensor values

1 Jan 03, 2022
A Python3 discord trojan, utilizing discord webhooks for sending information.

Vape-Lite-RAT A Python3 discord trojan, utilizing discord webhooks for sending information. What you do with this code / project / idea is non of my b

NightTab 12 Oct 15, 2022
Simple python script for automated network scans with random name generator(useful for CTF boxes).

📄 Automated NMAP script Description Simple python script for automated network scans with random name generator(useful for CTF boxes). Requirements 1

Dhmos Funk 2 Oct 29, 2021
A light-weight open-source project CLI utility for showing services running on ports in a host

Portable Port Scanner (ppscanner) Portable Port Scanner (ppscanner) is a light-weight open-source CLI utility that leverages on nmap to make quick and

1 Oct 30, 2021
LGPL Pure Python OPC-UA Client and Server

LGPL Pure Python OPC-UA Client and Server

Free OPC-UA Library 1.2k Jan 04, 2023
Ping Verification Python Script

Python Script Port Scanner Script WHAT IS IT? Port scanner script using Python. HOW IT WORKS Once the script has been executed, it will request the ta

AC 0 Dec 12, 2021
Repo used to maintain all notes and scripts developed during my DevNet Expert studies

DevNet Expert Studies Exam Date: TBD (Waiting for registration to open) This repository will be used to track my progress and maintain all notes/scrip

Dan 32 Dec 11, 2022
Using AWS's API Gateway + Lambda + Python to run a simple websocket application. For learning/testing

Using AWS's API Gateway + Lambda + Python to run a simple websocket application. For learning/testing. The AWS Resources seemed overly complex and were missing some critical gotchas in setting up a s

Seth Miller 15 Dec 23, 2022
This Python script can be used to bypass IP source restrictions using HTTP headers.

ipsourcebypass This Python script can be used to bypass IP source restrictions using HTTP headers. Features 17 HTTP headers. Multithreading. JSON expo

Podalirius 322 Dec 28, 2022
Multi-vendor library to simplify CLI connections to network devices

Netmiko Multi-vendor library to simplify CLI connections to network devices Why Netmiko? Network automation to screen-scraping devices is primarily co

Kirk Byers 3k Jan 01, 2023
This is a top level socket library, making servers and clients EASY!

quick-net Sockets don't have to be a pain That's the motto this library was built with, and that's exactly what we made! This is a top-level socket li

Nate the great 15 Dec 17, 2021
It's an extra broadcast driver for masonite. It adds support for socketio.

It's an extra broadcast driver for masonite. It adds support for socketio.

Yubaraj Shrestha 6 Feb 23, 2022
Enrich IP addresses with metadata and security IoC

Stratosphere IP enrich Get an IP address and enrich it with metadata and IoC You need API keys for VirusTotal and PassiveTotal (RiskIQ) How to use fro

Stratosphere IPS 10 Sep 25, 2022
A TrueCharts automatic and bulk update utility

trueupdate A TrueCharts automatic and bulk update utility How to install run pip install trueupdate Please be aware you will need to reinstall after e

TrueCharts 125 Jan 04, 2023
This tools just for education only - Layer-7 or HTTP FLOODER

Layer-7-Flooder This tools just for education only - Layer-7 or HTTP FLOODER Require Col1 Before You Run this tools How To Use Download This Source Ex

NumeX 7 Oct 30, 2022
Octodns-cloudflare - Cloudflare DNS provider for octoDNS

CloudflareProvider provider for octoDNS An octoDNS provider that targets Cloudfl

octoDNS 6 May 28, 2022
Synchronised text editor over TCP, for live editing with others.

SyncTEd Synchronised text editor over TCP, for live editing with others. Written in Python with PyGame. Run Install requirements: pip install -r requi

Marko Živić 1 May 13, 2022
Heroku Cloudflare App Domain

Heroku Cloudflare App Domain Creating branded herokuapp.com-like domains using Cloudflare, based on the app name (eg my-app-prod.example.com). Feature

Torchbox 2 Oct 04, 2022
A benchmark for stateful fuzzing of network protocols

A benchmark for stateful fuzzing of network protocols

3 Apr 25, 2022
A simple DHCP server and client simulation with python

About The Project This is a simple DHCP server and client simulation. I implemented it for computer network course spring 2021 The client can request

shakiba 3 Feb 08, 2022