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

前端 未结 7 1205
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:16

    If you want to avoid importing a module you can just apply a simple regex:

    • ^127.\d{1,3}.\d{1,3}.\d{1,3}$
    • ^10.\d{1,3}.\d{1,3}.\d{1,3}$
    • ^192.168.\d{1,3}$
    • ^172.(1[6-9]|2[0-9]|3[0-1]).[0-9]{1,3}.[0-9]{1,3}$

提交回复
热议问题