Skip to content

ipaddress - add ability to get next closest network of any prefix size #87027

Description

@777GE90
mannequin
BPO 42861
Nosy @ericvsmith, @777GE90
PRs
  • bpo-42861: Add next_network function to ipaddress library #24180
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2021-01-08.00:30:51.031>
    labels = ['type-feature', 'library', '3.10']
    title = 'ipaddress - add ability to get next closest network of any prefix size'
    updated_at = <Date 2021-07-17.22:04:30.115>
    user = 'https://github.com/777GE90'

    bugs.python.org fields:

    activity = <Date 2021-07-17.22:04:30.115>
    actor = 'eric.smith'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-01-08.00:30:51.031>
    creator = 'fasial.mahmood94'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42861
    keywords = ['patch']
    message_count = 4.0
    messages = ['384623', '384624', '384732', '386648']
    nosy_count = 3.0
    nosy_names = ['eric.smith', 'pmoody', 'fasial.mahmood94']
    pr_nums = ['24180']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue42861'
    versions = ['Python 3.10']

    Linked PRs

    Activity

    1. 777GE90 commented on Jan 8, 2021

      777GE90mannequin
      MannequinAuthor

      The ipaddress module in Python is an excellent tool, but I noticed it is missing a feature that I needed several months ago, which is the ability to find the next closest subnet with a specific prefix length.

      For example, imagine I had a IPv4Network("10.10.0.72/30"), how would I find the next possible /25 address? It is not the most straightforward thing to do, so think it would be a great enhancement to the ipaddress library.

      I think this can be achieved by adding in a new method to the BaseNetwork class, the method could be defined like "next_prefix(next_prefix=None)". Calling this method would return an IPv4/v6 address that is the closest possible match with the new prefix (defined as next_prefix).

      Example calls:
      v4 = IPv4Network("10.10.0.72/30")
      next_network = v4.next_subnet(next_prefix=25)
      # Output: next_network = IPv4Network("10.10.0.128/25")

      v4 = IPv4Network("10.10.0.72/30")
      next_network = v4.next_subnet(next_prefix=30)
      # Output: next_network = IPv4Network("10.10.0.76/30")
      
      v4 = IPv4Network("10.10.0.72/30")
      next_network = v4.next_subnet() # if next_prefix is not defined it will use the existing prefix of /30, so this call is exactly the same as the previous
      # Output: next_network = IPv4Network("10.10.0.76/30")
      
      v6 = IPv6Network("2001:db8:aaaa:aaaa:aaaa:aaaa:aaaa:0000/112")
      next_network = v6.next_subnet()
      # Output: next_network = IPv6Network("2001:db8:aaaa:aaaa:aaaa:aaaa:aaab:0/112")
      
      v6 = IPv6Network("2001:db8:aaaa:aaaa:aaaa:aaaa:aaaa:0000/112")
      next_network = v6.next_subnet(next_prefix=64)
      # Output: next_network = IPv6Network("2001:db8:aaaa:aaab::/64")

      I am going to be working on this and plan to raise a PR soon. This is my first time contributing to Python, so I appreciate your help / comments / suggestions / guidance as I go along.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Jan 8, 2021
    3. 777GE90 commented on Jan 8, 2021

      777GE90mannequin
      MannequinAuthor

      Not sure how to edit an issue, but I made a mistake, where I said:
      "...Calling this method would return an IPv4/v6 address that is the closest possible match..."

      I meant to say:
      "...Calling this method would return an IPv4/v6 -NETWORK- that is the closest possible match..."

    4. 777GE90 commented on Jan 9, 2021

      777GE90mannequin
      MannequinAuthor

      Updated the title and method naming, I previously mentioned the "next subnet" and called the method "next_subnet", but technically this is wrong. What this method should be doing is finding the next closest network of prefix size n.

      So I guess the method definition should be:
      next_network(self, next_network=None)

    5. changed the title [-]ipaddress - add ability to get next closet subnet of any prefix size[/-] [+]ipaddress - add ability to get next closet network of any prefix size[/+] on Jan 9, 2021
    6. 777GE90 commented on Feb 8, 2021

      777GE90mannequin
      MannequinAuthor

      Bump :)

    7. changed the title [-]ipaddress - add ability to get next closet network of any prefix size[/-] [+]ipaddress - add ability to get next closest network of any prefix size[/+] on Feb 15, 2021
    8. transferred this issue fromon Apr 10, 2022
    9. self-assigned this
      on Jul 8, 2026
    10. added
      3.16new features, bugs and security fixes
      and removed on Jul 8, 2026
    11. orsenthil commented on Jul 8, 2026

      @orsenthil
      Member

      Addressed here #24180

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.16new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions