@@ -26,34 +26,32 @@ Use `python-ipware` **only** as an additional layer to bolster your security, no
2626
2727# How to install
2828
29- 1. easy_install python-ipware
30- 2. pip install python-ipware
31- 3. git clone http:#github.com/un33k/python-ipware
32- a. cd python-ipware
33- b. run python setup.py install
34- 4. wget https:#github.com/un33k/python-ipware/zipball/master
35- a. unzip the downloaded file
36- b. cd into python-ipware-* directory
37- c. run python setup.py install
29+ ```
30+ pip install python-ipware
31+ ```
32+ -- or --
33+ ```
34+ pip3 install python-ipware
35+ ```
3836
3937# How to use
4038
41- ### Using ipware to Retrieve Client IP in Django or Flask
39+ ### Using python- ipware to Retrieve Client IP in Django or Flask
4240
43- Here's a basic example of how to use ` ipware ` in a view or middleware where the ` request ` object is available. This can be applied in Django, Flask, or other similar frameworks.
41+ Here's a basic example of how to use ` python- ipware` in a view or middleware where the ` request ` object is available. This can be applied in Django, Flask, or other similar frameworks.
4442
4543``` python
46- from ipware import Ipware
44+ from ipware import IpWare
4745
48- # Instantiate Ipware with default values
49- ipware = Ipware ()
46+ # Instantiate IpWare with default values
47+ ipw = IpWare ()
5048
5149# Get the META data from the request object
52- # For Django use request.META, for Flask use request.environ
53- meta = request.META
50+ meta = request.META # Django
51+ # meta = request.environ # Flask
5452
5553# Get the client IP and the trusted route flag
56- ip, trusted_route = ipware .get_client_ip(meta)
54+ ip, trusted_route = ipw .get_client_ip(meta)
5755
5856if ip:
5957 # The 'ip' is an object of type IPv4Address() or IPv6Address() with properties like:
@@ -124,18 +122,16 @@ You can customize the order by providing your own list during initialization whe
124122
125123``` python
126124# specific meta key
127- ipware = IpWare(precedence = (" X_FORWARDED_FOR" ))
125+ ipw = IpWare(precedence = (" X_FORWARDED_FOR" ))
128126
129127# multiple meta keys
130- ipware = IpWare(precedence = (" X_FORWARDED_FOR" , " HTTP_X_FORWARDED_FOR" ))
131-
132- # usage is just to pass in the http request headers
128+ ipw = IpWare(precedence = (" X_FORWARDED_FOR" , " HTTP_X_FORWARDED_FOR" ))
133129
134130# Django (request.META)
135- ip, proxy_verified = ipware .get_client_ip(meta = request.META )
131+ ip, proxy_verified = ipw .get_client_ip(meta = request.META )
136132
137133# Flask (request.environ)
138- ip, proxy_verified = ipware .get_client_ip(meta = request.environ)
134+ ip, proxy_verified = ipw .get_client_ip(meta = request.environ)
139135
140136# ... etc.
141137
@@ -151,26 +147,26 @@ You can pass your custom list on every call, when calling the proxy-aware api to
151147
152148``` python
153149# In the above scenario, use your load balancer IP address as a way to filter out unwanted requests.
154- ipware = IpWare(proxy_list = [" 198.84.193.157" ])
150+ ipw = IpWare(proxy_list = [" 198.84.193.157" ])
155151
156152
157153# If you have multiple proxies, simply add them to the list
158- ipware = IpWare(proxy_list = [" 198.84.193.157" , " 198.84.193.158" ])
154+ ipw = IpWare(proxy_list = [" 198.84.193.157" , " 198.84.193.158" ])
159155
160156# For proxy servers with fixed sub-domain and dynamic IP, use the following pattern.
161- ipware = IpWare(proxy_list = [" 177.139." , " 177.140" ])
157+ ipw = IpWare(proxy_list = [" 177.139." , " 177.140" ])
162158
163159# usage: non-strict mode (X-Forwarded-For: <fake>, <client>, <proxy1>, <proxy2>)
164160# The request went through our <proxy1> and <proxy2>, then our server
165161# We choose the <client> ip address to the left our <proxy1> and ignore other ips
166- ip, trusted_route = self .ipware .get_client_ip(meta = request.META )
162+ ip, trusted_route = ipw .get_client_ip(meta = request.META )
167163
168164
169165# usage: strict mode (X-Forwarded-For: <client>, <proxy1>, <proxy2>)
170166# The request went through our <proxy1> and <proxy2>, then our server
171167# Total ip address are total trusted proxies + client ip
172168# We don't allow far-end proxies, or fake addresses (exact or None)
173- ip, trusted_route = self .ipware .get_client_ip(meta = request.META , strict = True )
169+ ip, trusted_route = ipw .get_client_ip(meta = request.META , strict = True )
174170```
175171
176172In the following ` example ` , your public load balancer (LB) can be seen as a ` trusted ` proxy.
@@ -190,23 +186,23 @@ You can customize the proxy count by providing your `proxy_count` during initial
190186
191187``` python
192188# In the above scenario, the total number of proxies can be used as a way to filter out unwanted requests.
193- import ipware
189+ from ipware import IpWare
194190
195191# enforce proxy count
196- ipware = IpWare(proxy_count = 1 )
192+ ipw = IpWare(proxy_count = 1 )
197193
198194# enforce proxy count and trusted proxies
199- ipware = IpWare(proxy_count = 1 , proxy_list = [" 198.84.193.157" ])
195+ ipw = IpWare(proxy_count = 1 , proxy_list = [" 198.84.193.157" ])
200196
201197
202198# usage: non-strict mode (X-Forwarded-For: <fake>, <client>, <proxy1>, <proxy2>)
203199# total number of ip addresses are greater than the total count
204- ip, trusted_route = self .ipware .get_client_ip(meta = request.META )
200+ ip, trusted_route = ipw .get_client_ip(meta = request.META )
205201
206202
207203# usage: strict mode (X-Forwarded-For: <client>, <proxy1>, <proxy2>)
208204# total number of ip addresses are exactly equal to client ip + proxy_count
209- ip, trusted_route = self .ipware .get_client_ip(meta = request.META , strict = True )
205+ ip, trusted_route = ipw .get_client_ip(meta = request.META , strict = True )
210206```
211207
212208In the following ` example ` , your public load balancer (LB) can be seen as the ` only ` proxy.
@@ -223,12 +219,12 @@ In the following `example`, your public load balancer (LB) can be seen as the `o
223219``` python
224220# We make best attempt to return the first public IP address based on header precedence
225221# Then we fall back on private, followed by loopback
226- import ipware
222+ from ipware import IpWare
227223
228224# no proxy enforce in this example
229- ipware = IpWare()
225+ ipw = IpWare()
230226
231- ip, _ = ipware .get_client_ip(meta = request.META )
227+ ip, _ = ipw .get_client_ip(meta = request.META )
232228
233229if ip.is_global:
234230 print (' Public IP' )
@@ -249,7 +245,7 @@ else if ip.is_reserved:
249245
250246#### Support for IPv4, IPv6, and IP: Port Patterns
251247
252- ` ipware ` is designed to handle various IP address formats efficiently:
248+ ` python- ipware` is designed to handle various IP address formats efficiently:
253249
254250- ** Ports Stripping:** Automatically removes ports from IP addresses, ensuring only the IP is processed.
255251- ** IPv6 Unwrapping:** Extracts and processes IPv4 addresses wrapped in IPv6 containers.
0 commit comments