I want to check my network client’s IP address. If the client uses HTTP, I can get its IP address by letting it connect http://ipinfo.io/ip or the like. But I want to prevent it from using HTTP. Is there any TCP service (server) that acts like http://ipinfo.io/ip but doesn’t require HTTP?
For example, I want a server running this python script forever:
import socket
EXAMPLE_PORT = 50007
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', EXAMPLE_PORT))
s.listen(1)
while True:
conn, addr = s.accept()
with conn:
conn.sendall(addr[0].encode())
And I want to run the next python script as my client to get its IP address:
import socket
EXAMPLE_SERVER = 'theserver.xyz'
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((EXAMPLE_SERVER, 50007))
data = s.recv(1024)
print("my client's IP address:", repr(data))
Notes:
- I can make the server myself. But I want to know public service if it already exists.
- My client runs on hosts that I don’t know their IP addresses.
dig +short myip.opendns.com @resolver.opendns.com
I could get my client’s IP address by your method. I didn’t know such a dns server echoing IP addresses. Thanks, info! Btw, I couldn’t reach resolver.opendns.com. So I used this:
dig +short myip.opendns.com @resolver1.opendns.com
dig +short myip.opendns.com @resolver2.opendns.com