0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PythonでIPアドレスを取得する方法

Last updated at Posted at 2022-07-04

1つのIPアドレスのみ活性化している場合

import socket
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)

結果

10.56.126.27

文字列とて返されます。

複数のIPアドレスが活性化している場合

import socket
hostname = socket.gethostname()
hostname, alias_list, ipaddr_list = socket.gethostbyname_ex(hostname)
print(ipaddr_list)

結果

['10.56.126.27', '10.202.6.27', '10.156.6.27']

List型で返されます。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?