Python ip segment Processing

Source: Internet
Author: User

Requirement: ip segment 255.255.255.255.20-255.21.11.11

-------------------------------------------------------

Write it down for future reuse or peers who need it ~

# Generate the boundary condition def get_side (pos, now) for the next loop: global begin, end global first, last # find_side # --------- in --------------- # begin 110.67.1.95 end 111.67.1.95 # first 110. x. x. x last 111. x. x. x # pos 110 now 0 # --------- out ------------ # first 110.67.x.x last 111.20.x.x # find_side = lambda begin, end, pos, fisrt, last, now: \ (pos = first [now] and begin [now + 1] or 1, pos = last [now] and end [now + 1] or 255) first [now + 1], last [now + 1] = find_side (begin, end, pos, first, last, now) return range (first [now + 1], last [now + 1] + 1) def gen_ip (ip): ''' in 110.67.1.30-110.67.1.32 out' {"110.67.1.30", "110.67.1.31 ", "110.67.1.33"} ''' # ip segment range: global begin, end # temporary list, storage boundary condition: global first, last sides = ip. split ('-') begin = map (lambda x: int (x), sides [0]. split ('. ') end = map (lambda x: int (x), sides [1]. split ('. ')) first [0] = begin [0] last [0] = end [0] ip_str = "'{% s}'" tmp = "" for pos_one in range (begin [0 ], end [0] + 1): for pos_two in get_side (pos_one, 0): for pos_three in get_side (pos_two, 1): for pos_four in get_side (pos_three, 2 ): str = '"% d. % d. % d. % d ", '% (pos_one, pos_two, pos_three, pos_four) tmp + = str return ip_str % tmp. rstrip (',')

The idea is to start the loop from the first part of the ip segment. There are four layers of loops in total.

There is only one problem to be aware of during the entire conversion. How to correctly process the Boundary Value in the loop.

For example, 10.10.10.10-10.10.20.1

In the third field, perform the following cycles: 10.10-10.255; 11.1-11.255... 19.255; 20.1

There are three situations in which the starting, transition, and termination fields of the current loop are considered.

Therefore, line #14 performs two ternary operations, which are roughly similar to pos = from? Begin + 1: 1, pos =? End + 1: 255

Write the corresponding boundary according to whether the content of the current loop is consistent with the start and end segments.

Of course, in special cases 1. the first two ip segments are different. When the last two IP segments are consistent, some data is missing. However, this bug is ignored because the ip range I used in this project cannot reach the following range.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.