Created
November 26, 2014 10:50
-
-
Save wangtai/c02662f89bcd0a7b45c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_ip(request): | |
try: | |
real_ip = request.META['HTTP_X_FORWARDED_FOR'] | |
reg_ip = real_ip.split(",")[0] | |
except KeyError: | |
try: | |
reg_ip = request.META['REMOTE_ADDR'] | |
except KeyError: | |
return 0 | |
ip_list = reg_ip.split('.') | |
logging.debug(repr(ip_list)) | |
ans = 0 | |
for item in ip_list: | |
ans = (ans << 8) + int(item) | |
return ans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment