anantdd
anantdd

Reputation: 135

IP and port of current server in django

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

Answers (2)

Kshitij Saxena
Kshitij Saxena

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

Niranj Rajasekaran
Niranj Rajasekaran

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

Related Questions