Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using the low level networking interface you are actually getting the address of the server the python interpreter is running on:</p> <p>"<em>socket.gethostname(): Return a string containing the hostname of the machine where the Python interpreter is currently executing.</em>"</p> <p><strong>Getting the client ip using low-level network interface</strong></p> <p>"<em>socket.accept() Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.</em>"</p> <pre><code># Echo server program import socket HOST = '' # Symbolic name meaning all available interfaces PORT = 50007 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) if not data: break conn.send(data) conn.close() </code></pre> <p><a href="http://docs.python.org/library/socket.html" rel="nofollow noreferrer">More info on the low level network interface</a></p> <p><strong>Getting the client IP from HTTP headers (if serving html)</strong></p> <p><code>REMOTE_ADDR</code> is the header that contains the clients ip address you should check that first.</p> <p>You should also check to for the <code>HTTP_X_FORWARDED</code> header in case you're visitor is going through a proxy. Be aware that <code>HTTP_X_FORWARDED</code> is an array that can contain multiple comma separated values depending on the number of proxies.</p> <p>Also be aware that you may be NATed (<a href="http://en.wikipedia.org/wiki/Network_address_translation" rel="nofollow noreferrer">Network Address Translation</a>). If your ip is internal (10.x.x.x or 192.168.x.x to name a few) you are definitely behind a NAT router and only your external ip will be exposed to websites.</p> <p>Here is a small c# snippet that shows determining the client's ip, the logic is easy to convert the python and the server headers are the same:</p> <pre><code>string clientIp = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if( !string.IsNullOrEmpty(clientIp) ) { string[] forwardedIps = clientIp.Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ); clientIp = forwardedIps[forwardedIps.Length - 1]; } else { clientIp = context.Request.ServerVariables["REMOTE_ADDR"]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload