Mack
Mack

Reputation: 335

Is it possible to obtain the IP address from a HTTP Request

I am trying to identify users that access my website. I know of assigning a cookie to them & identifying them that way. But is it also possible to use their IP address to identify them?

I know of IP subclassing (is that the correct term) so if many users on the same network access my site & have subclassing, they will all have the same IP address but thats ok, I dont want my IP identification to be exact, its just a backup if the user has no cookie.

If I have a HTTP request can I get the ip address of the requester/sender? For example to get the browser type I check the HTTP (header?) user-agent. With my little knowledge of Data Communications, if the HTTP request does contain an IP address, wont it have the IP address of the last hop(router/switch) or am I thinking of TCP?

I am using Python & cgi; so is there a way to determine the HTTP requesters IP address either by looking at the HTTP request, or maybe TCP packets(I never worked with TCP in python, how could I look at data packets in python?).

Upvotes: 0

Views: 2807

Answers (1)

Alex
Alex

Reputation: 65934

Sure. Use cgi, and os. The client's IP address is located in the environ variable with a title of REMOTE_ADDR. For example, the following will print out the client's ip address:

import cgi
import os

print "Content-type: text/html"
print ""
print cgi.escape(os.environ["REMOTE_ADDR"])

Upvotes: 1

Related Questions