Django-Forwarded
Support HTTP "Forwarded" header in your Django applications.
This middleware for Django adds support for the Forwarded header which is standardized by Internet Engineering Task Force (IETF) in RFC 7239 and summarized in Mozilla Developer Network (MDN) article of the same name. This header is used by many reverse HTTP proxies to pass client identification details (such as IPv4 or IPv6) to the backend application.
Installation
Install the middleware with your favorite Python package manager
Pip:
$ pip install django-forwarded
Pipenv:
$ pipenv install django-forwarded
Usage
Activate
Add the middleware to Django's MIDDLEWARE setting.
MIDDLEWARE = (
... # some middlewares
"django_forwarded.Forwarded",
... # more middlewares
)
Configure
You can configure Django-Forwarded by adding appropriate variable to your Django application configuration file.
With TRUSTED_PROXY_LIST
Just specify a list of trusted proxies with the TRUSTED_PROXY_LIST variable in the file. This is the recommended way to specify the proxies.
TRUSTED_PROXY_LIST = [
'2001:db8::10',
'10.2.3.100',
]
With TRUSTED_PROXY_DEPTH
Just specify the number of trusted proxies with the TRUSTED_PROXY_DEPTH variable. This is useful when you have no control over them and their IP(s) might change frequently over the lifetime of your application.
TRUSTED_PROXY_DEPTH = 2 # trusts maximum of 2 proxies
Note: It is not secure as secure as specifying a list.
Access client IP
The middleware identifies the correct client IP from the user supplied configuration and the header received from the proxies. This information is then placed in the request.META['REMOTE_ADDR'] field.
def some_function(request):
print(request.META['REMOTE_ADDR'])
Testing
To execute the included test suit, run the following commands in a terminal:
$ cd <path to the django-forwarded>
$ pipenv shell # opens a shell in dev environment
$ DJANGO_SETTINGS_MODULE=tests.settings python3 -m unittest # run the tests
Release files for Django-Forwarded 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| Django-Forwarded-0.0.2.tar.gz | 5.2 kB | Details |
Release files / Django-Forwarded-0.0.2.tar.gz
| Download URL | Django-Forwarded-0.0.2.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
824686f4341f613097e27b51c8f9fb3b9239269fcba4b7009fa0cf7f618c1a76
|
|
BLAKE2b-256 checksum How to use checksums |
359ae4d962c172312c4970c7aef787b063c08418662074d69b7c6c5e9df1b280
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.5
|