Skip to main content

sanic-ipware

This is a fork of django-ipware to work with Sanic.

Overview

Best attempt to get client’s IP address while keeping it DRY.

Notice

There is no real good “out-of-the-box” solution against fake IP addresses, aka “IP Address Spoofing”. You are encouraged to read the Advanced users section of this page and use trusted_proxies_ips and/or proxy_count features to match your needs, especially if you are planning to include sanic-ipware in any authentication, security or “anti-fraud” related architecture.

How to install

The best way to install sanic-ipware would be using pip:

pip install sanic-ipware

How to use

There’s basically one method that should be usable from sanic_ipware, called get_client_ip. The result is a Tuple[Optional[str], bool] of (ipaddr, routable).

from sanic_ipware import get_client_ip

@app.get("/some/handler")
async def somehandler(request):
    ip, routable = get_client_ip(request)
    if ip is not None:
        if routable:
            # we have a (probably) real, public ip address for user
        else:
            # we have ip address, but it might not be public routable
    else:
        # we don't have a ip address for the user

Advanced users

# you can provide your own meta precedence order by using the
# request_header_order in the function call:
ip, routable = get_client_ip(
    request,
    request_header_order=['Forwarded-For', 'X-Forwarded-For'])

# if you're going to do this a lot, wrap the function somewhere with
# functools.partial
from functools import partial
my_get_client_ip = partial(
    get_client_ip,
    request_header_order=['Forwarded-For', 'X-Forwarded-For'])
ip, routable = my_get_client_ip(request)

# if you plan to use sanic_ipware in any authentication, security or
# "anti-fraud" related architecture, you should configure it to only
# "trust" one or more "known" proxy server(s)), in the function call:
ip, routable = get_client_ip(request, proxy_trusted_ips=['198.84.193.158'])

# you can perform the same functools.partial trick with these trusted IPs

License

MIT, the same as django-ipware license .

Changelog

v0.1.0 on 2018-09-28

  • First release on PyPI. Not stable.

Metadata

Release files for sanic-ipware 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic-ipware 0.1.0
File Size Uploaded Provenance
sanic-ipware-0.1.0.tar.gz 14.0 kB Without provenance Details

Release files / sanic-ipware-0.1.0.tar.gz

Download URL sanic-ipware-0.1.0.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
43a1894e2ce3b68175319274afc29d817dbf9100d165705d0be0b71670eed227
BLAKE2b-256 checksum
How to use checksums
355cad4f2207b5ae8f7156b8fbb089591d1a2730a67dcd7eb7e7e865210c7374
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

Security for sanic-ipware 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page