In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people don't know what to do about how to use request to get the requested IP address in django. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Install third-party libraries
Pip install django-ipware
Called in view
General usage:
From ipware.ip import get_ip # Import package def view_test (request): ip = get_ip (request) # get request request IP
Copy
Site-packages/ipware/ip.py source code:
From .utils import is_valid_ipfrom. Import defaults as defsNON_PUBLIC_IP_PREFIX = tuple ([ip.lower () for ip in defs.IPWARE_NON_PUBLIC_IP_PREFIX]) TRUSTED_PROXY_LIST = tuple ([ip.lower () for ip in defs.IPWARE_TRUSTED_PROXY_LIST]) def get_ip (request, real_ip_only=False, right_most_proxy=False): "" Returns client's best-matched ip-address Or None "" best_matched_ip = None for key in defs.IPWARE_META_PRECEDENCE_ORDER: value = request.META.get (key, request.META.get (key.replace ('_','-'),''). Strip () if value is not None and value! ='': ips = [ip.strip () .lower () for ip in value.split (') ')] if right_most_proxy and len (ips) > 1: ips = reversed (ips) for ip_str in ips: if ip_str and is_valid_ip (ip_str): if not ip_str.startswith (NON_PUBLIC_IP_PREFIX): return ip_str If not real_ip_only: loopback = defs.IPWARE_LOOPBACK_PREFIX if best_matched_ip is None: best_matched_ip = ip_str elif best_matched_ip.startswith (loopback) and not ip_str.startswith (loopback): Best_matched_ip = ip_str return best_matched_ipdef get_real_ip (request Right_most_proxy=False): "" Returns client's best-matched `real`` externally- routable` ip-address, or None "" return get_ip (request, real_ip_only=True, right_most_proxy=right_most_proxy) def get_trusted_ip (request, right_most_proxy=False) Trusted_proxies=TRUSTED_PROXY_LIST): "Returns client's ip-address from `roomd` proxy server (s) or None"if trusted_proxies: meta_keys = ['HTTP_X_FORWARDED_FOR',' XanomalFORWARDEDFOR'] for key in meta_keys: value = request.META.get (key, request.META.get (key.replace ('_','-')) ) .strip () if value: ips = [ip.strip () .lower () for ip in value.split (' ')] if len (ips) > 1: if right_most_proxy: ips.reverse () for proxy in trusted_proxies: if proxy in ips [- 1]: return ips [0] return None
Copy
Purpose: save and access IP with decorator
Import refrom django.core.cache import cachefrom django.shortcuts import renderfrom ipware.ip import get_ipdef get_ipv4 (ip): "" get IPv4: param ip:: return: "" # IP patterns ipv4_re = r'(?: 25 [0-5] | 2 [0-4]\ d | [0-1]?\ d?\ d) (?:\. (?: 25 [0-5] | 2 [0-4]\ d | [0-4]\ d 1]?\ d?\ d) {3}'# ipv6_re = r'\ [[0-9a-f:\.] +\]'# (simple regex Validated later) ipv4 = re.search (ipv4_re, ip) if ipv4: return ipv4.group () return ipdef save_ip (ip): "Save IP: param ip:: return:" ip = get_ipv4 (ip) cache_ip = cache.get (ip) if not cache_ip: cache.set (ip, int (time.time () CACHE_TIMEOUT_ARTICLE) visit_status = UserIP.objects.filter (ip=ip). Exists () if visit_status: ip_info = UserIP.objects.get (ip=ip) ip_info.visit_num + = 1 ip_info.save (update_fields= ["visit_num", "time_updated"]) else: ip_info = UserIP (ip=ip Location=get_ip_location (ip), visit_num=1,) ip_info.save () # this is a function of a decorator The outer function is used to receive the def save_visit_ip (func) of the decorated function: "" when accessing the view function, save the access ip: param func:: return: "def inner (request, * args, * * kwargs): ip = get_ip (request) save_ip (ip) return func (request, * args) * * kwargs) return inner@save_visit_ipdef status_code (request): code = request.GET.get ("code", None) status_code = {"200": "
Access is normal
"403": "
Access denied
"404": "
Resource not found
"500": "
Server internal error
"503": "
Server maintenance
",} if code in status_code.keys (): response = HttpResponse (status_ code) response.status_code = int (code) return response return render (request," web_status_code.html ", locals ()) after reading the above, have you mastered how to use request to obtain the requested IP address in django? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.