Skip to content

Repository files navigation

IPForce Logo

IPForce: Force IPv4/IPv6 in Python HTTP Requests


PyPI version built with Python3 GitHub repo size Discord Channel

Overview

IPForce is a Python library that provides HTTP adapters for forcing specific IP protocol versions (IPv4 or IPv6) during HTTP requests. It's particularly useful for testing network connectivity, ensuring compatibility with specific network configurations, and controlling which IP protocol version is used for DNS resolution and connections.

PyPI Counter
Github Stars
Branch main dev
CI
Code Quality CodeFactor

Installation

Source Code

PyPI

Usage

Enforce IPv4

Use when you need to ensure connections only use IPv4 addresses, useful for legacy systems that don't support IPv6, networks with IPv4-only infrastructure, or testing IPv4 connectivity.

from ipforce import IPForceAdapter, IPVersion, IPForceMethod
import requests

session = requests.Session()
adapter = IPForceAdapter(IPVersion.V4, IPForceMethod.LOCK)
session.mount('http://', adapter)
session.mount('https://', adapter)

response = session.get('https://ifconfig.co/json')

Enforce IPv6

Use when you need to ensure connections only use IPv6 addresses, useful for modern networks with IPv6 infrastructure, testing IPv6 connectivity, or applications requiring IPv6-specific features.

from ipforce import IPForceAdapter, IPVersion, IPForceMethod
import requests

session = requests.Session()
adapter = IPForceAdapter(IPVersion.V6, IPForceMethod.LOCK)
session.mount('http://', adapter)
session.mount('https://', adapter)

response = session.get('https://ifconfig.co/json')

Using IPForceSession

from ipforce import IPForceSession, IPVersion

with IPForceSession(IPVersion.V4) as session:
    response = session.get('https://ifconfig.co/json')

Available Methods

Method Description
IPForceMethod.LOCK Thread-safe — global lock serialization (default)
IPForceMethod.GLOBAL Non-thread-safe — temporary getaddrinfo patch

Warning

IPForceMethod.GLOBAL is NOT thread-safe. It modifies the global socket.getaddrinfo function, which can cause race conditions in multi-threaded applications. Use IPForceMethod.LOCK (the default) for concurrent usage.

Direct Class Usage (Deprecated)

The following direct class usage still works but is deprecated in favor of the unified API above:

from ipforce import IPv4LockAdapter, IPv6LockAdapter

session = requests.Session()
session.mount('http://', IPv4LockAdapter()) # or IPv6LockAdapter()
session.mount('https://', IPv4LockAdapter()) # or IPv6LockAdapter()
response = session.get('https://ifconfig.co/json')

Issues & Bug Reports

Just fill an issue and describe it. We'll check it ASAP!

  • Please complete the issue template

You can also join our discord server

Discord Channel

Show Your Support

Star This Repo

Give a ⭐️ if this project helped you!

Donate to Our Project

If you do like our project and we hope that you do, can you please support us? Our project is not and is never going to be working for profit. We need the money just so we can continue doing what we do ;-)

IPForce Donation

About

IPForce: Force IPv4/IPv6 in Python HTTP Requests

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

9 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Contributors

Languages