How to get user IP addresses using django in Python-Python tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.