28
25

More than 5 years have passed since last update.

Python でホスト名を取得する

Last updated at Posted at 2012-12-26

色々な環境でを動かしていたり、アプリケーションサーバが複数ある場合に、どの環境からメールが送信されたのかわかりづらい。

なのでメールのサブジェクトにホスト名を含めて送信する。
socketモジュールでホスト名を取得することができるので、これをプレフィックスにしてメールを送信する。

socket

settings.py
from socket import gethostname
EMAIL_SUBJECT_PREFX = '[%s]' % gethostname()

os

またはosモジュールのunameから取得することもできる。

settings.py
import os
EMAIL_SUBJECT_PREFIX = '[%s]' % os.uname()[1]

platform

同様にplatformモジュールでも。

settings.py
import platform
EMAIL_SUBJECT_PREFIX = '[%s]' % platform.uname()[1]
28
25
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
25