How do you determine if an IP address is private, in Python?

前端 未结 7 1213
Happy的楠姐
Happy的楠姐 2020-12-02 18:48

In Python, what is the best way to determine if an IP address (e.g., \'127.0.0.1\' or \'10.98.76.6\') is on a private network? The code does not s

7条回答
  •  既然无缘
    2020-12-02 19:21

    Since Python 3.3 there is an ipaddress module in the stdlib that you can use.

    >>> import ipaddress
    >>> ipaddress.ip_address('192.168.0.1').is_private
    True
    

    If using Python 2.6 or higher I would strongly recommend to use a backport of this module.

提交回复
热议问题