ホスト名(コンピュータ名)の取得方法
socket.gethostname() メソッドを呼ぶと、ホスト名 を文字列で返します。import socket
ホスト名 = socket.gethostname()
- gethostname() を使用するには socket モジュールのインポートが必要です。
import socket
# ホスト名の取得
hostName = socket.gethostname()
# 取得したホスト名を表示
print(f'socket.gethostname() = {hostName}')
- 4行目 ホスト名 を取得しています。

検証環境
- Python 3.9.7 (default, Sep 3 2021, 20:10:26) [GCC 10.2.1 20210110] on linux
- Docker image: python
- Docker Desktop 4.0.1(68347)
- Docker Engine 20.10.8
- Docker Compose v2.0.0-rc.3
- Microsoft Windows 10 Pro Version 21H1 OS Build 19043.1237 Experience: Windows Feature Experience Pack 120.2212.3530.0