如何在Python Django中获取服务器名称?我们在AWS-Apache机器上托管了Django应用程序,该机器具有1台以上的服务器,并根据负载有所增加。 现在,我需要查找从哪个服务器发出特定请求,以及从哪个服务器获得响应。
我在AWS机器上使用Python 3.6,Django,Django restframework和Apache服务器。
答案 0 :(得分:1)
我假设服务器名称是主机名,
要获取您的主机名,您可以使用
import socket
socket.gethostname()
https://docs.python.org/3/library/socket.html#socket.gethostname
或者您可以查询实例元数据,在这里您将获得更丰富的结果,例如
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
我的意见是尝试使用instance-id