This article mainly introduces how to use django to obtain the IP address of a user in Python. The example analyzes the problems and corresponding solutions in obtaining the IP address of a user by django, which is very simple and practical, for more information about how to use django to obtain user IP addresses, see the example in this article. Share it with you for your reference. The details are as follows:
Function implementation:
Def get_client_ip (request): try: real_ip = request. META ['http _ X_FORWARDED_FOR '] regip = real_ip.split (",") [0] destination T: try: regip = request. META ['remote _ ADDR '] metadata T: regip = "" return regip
The problem is that the request. META has no key value equal to HTTP_X_FORWARDED_FOR and REMOTE_ADDR. The server is implemented using nginx + fastcgi.
Solution:
Add the following to fastcgi configuration of nginx:
Fastcgi_param REMOTE_ADDR $ remote_addr;
I hope this article will help you with Python programming.