Reputation: 135
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?
Upvotes: 1
Views: 1811
Reputation: 940
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.
Upvotes: 2
Reputation: 778
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)
Upvotes: 1