Sergey Potekhin
Sergey Potekhin

Reputation: 701

How to get my IP with Python 3, socket and proxy (TOR)?

I've tried to use some third-party services like ipify.com, but it's pretty easy to get Max retries exception, because I'm sending a lot of requests.

So I've found a really simple way to get my IP through Google DNS (link). Here's the code:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
print(s.getsockname()[0])
s.close()

Also, I've read a few articles (link 1, link 2, link 3) and in my understanding, it's pretty easy to send HTTP GET request via proxy with socket module.

But I can't understand, how can I merge this two techniques and connect to 8.8.8.8 via proxy.

Need your help, thanks in advance.

Upvotes: 1

Views: 587

Answers (1)

George Y.
George Y.

Reputation: 11779

You cannot merge those techniques because you're using DGRAM (UDP) sockets, and Tor only proxies TCP connections.

So you need to connect to a TCP-based server. The best choice would be your own server - it is fairly easy to set up something on AWS dumping SERVER_IP, and depending on your load it probably won't cost much - if anything. Or you can use free IP query servers, but rotate them (using different server in each request), to ensure you won't exceed the allowed threshold.

Upvotes: 1

Related Questions