3
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アドレスをGETする

Posted at

次のコードだと、 127.0.1.1 になる。ほしいのはコレじゃない。テックアカデミー等の記事が検索されるけど、ちゃんと書いてほしい。


import socket
# ホスト名を取得、表示
host = socket.gethostname()
print(host)

# ipアドレスを取得、表示
ip = socket.gethostbyname(host)
print(ip) # 192.168.○○○.○○○

pip install ipgetしてから、

    a = ipget.ipget()
    print(a.ipaddr("eth0")) # 172.xx.xx.xx/24 など

これで取得できる。

3
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
3
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?