I am writing a little Python script that checks and displays incoming IP addresses. Getting an IP address directly was achieved by simply using the following:
remote_addr2 = os.environ.get('REMOTE_ADDR')
This displays the correct IP address making the request. The challenge I am facing is to detect the original IP if the request is made using a proxy. From here, I gather I must use HTTP_X_FORWARDED_FOR
and so I tried to assign that to a variable and display it, like so:
remote_addr1 = os.environ.get('HTTP_X_FORWARDED_FOR')
The output for this is always None
. I see a similar question answered for where the OP was using PHP. I think what I am trying to achieve in the second line is similar, but every proxy that I've tested (Anonymouse, HideMyAss, FilterByPass), gives me 'None' as an output.
This is the full program (rudimentary and I haven't implemented validation yet):
#!/usr/bin/python
import os
import cgi
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>WHOIS - TESTs</title>'
print '</head>'
remote_addr1 = os.environ.get('HTTP_X_FORWARDED_FOR')
remote_addr2 = os.environ.get('REMOTE_ADDR')
print '<body>'
print remote_addr1
print remote_addr2
#print cgi.escape(os.environ["REMOTE_ADDR"])
print '</body>'
print '</html>'
Sample output:
Where am I making a mistake? Thanks in advance.