0

How to get the server name in Python Django? we hosted Django application in AWS - Apache machine which have more than 1 server and increased based on load. Now i need to find from which server the particular request is made and from which server we got the response.

I am using Python 3.6, Django,Django restframework, Apache server on AWS machine.

Andrew
  • 8,322
  • 2
  • 47
  • 70
Ramesh
  • 51
  • 7
  • Does this answer your question? [How to use Django to get the name for the host server?](https://stackoverflow.com/questions/4093999/how-to-use-django-to-get-the-name-for-the-host-server) – Andrew Apr 29 '20 at 23:55

1 Answers1

1

I assume by server name you mean hostname,

to get your hostname you can use

import socket
socket.gethostname()

https://docs.python.org/3/library/socket.html#socket.gethostname

or you can query your instance metadata, here you'll get a richer result such as

ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
events/
hostname
iam/
instance-action
instance-id
instance-type
local-hostname
local-ipv4
mac
metrics/
network/
placement/
profile
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups
services/

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html

My opinion is try to use instance-id

ThatBuffDude
  • 451
  • 4
  • 11