I want to user the current IP and port as a variable at multiple servers in Django. I have tried calling api.ipify.org to get IP but is there any better way without making an api call?
Asked
Active
Viewed 1,603 times
1
-
Possible duplicate of [Getting a machine's external IP address with Python](https://stackoverflow.com/questions/2311510/getting-a-machines-external-ip-address-with-python) – Anton Bryzgalov Mar 22 '19 at 09:08
2 Answers
2
The best way to get your External IP is to use an external service like you're doing.
import requests
your_external_ip = requests.get('https://api.ipify.org').json()['ip']
Your port will be defined by your server's routing configurations. I'd suggest read the port from that file. However, this IP will route you to your configured port, you don't need to specify it.

Kshitij Saxena
- 930
- 8
- 19
1
Hope this helps
import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("Your Computer Name is:" + hostname)
print("Your Computer IP Address is:" + IPAddr)

Niranj Rajasekaran
- 740
- 9
- 20
-
This is not the external IP. I need an address that can be called from any client. – anantdd Mar 22 '19 at 09:05