Skip to main content

Retrieve your client's IP address (or try its best to)

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sanic-ipware-0.1.0.tar.gz (14.0 kB view details)

Uploaded Source

File details

Details for the file sanic-ipware-0.1.0.tar.gz.

File metadata

  • Download URL: sanic-ipware-0.1.0.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for sanic-ipware-0.1.0.tar.gz
Algorithm Hash digest
SHA256 43a1894e2ce3b68175319274afc29d817dbf9100d165705d0be0b71670eed227
MD5 fc10027ce115d43441d5691cbb7140fd
BLAKE2b-256 355cad4f2207b5ae8f7156b8fbb089591d1a2730a67dcd7eb7e7e865210c7374

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page