CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2007
    Posts
    15

    Python Line parser

    Hi all
    I need to create a python program that will read in lines from a text file in the format:
    XXX.XXX.XXX.XXX-XXX
    These are ip address that we have to verify that they are valid.
    We are given a range of ip address and need to scan them all so how woul dI go about striping the last part off and scan through that range to check them all.
    Any help would be great.
    I can open the file and read the lines that is not a problem.

  2. #2
    Join Date
    Mar 2008
    Posts
    5

    Re: Python Line parser

    If I think I know what you mean, this code should extract the parts of one line and iterate through a range of IP's, passing each one to a function I'm presuming you already have called "check_ip" which takes an IP in string format.

    Code:
    parts = line.split(".")
    first_part = parts[:-1]
    last_part = parts[-1]
    the_range = last_part.split("-")
    bottom = the_range[0]
    top = the_range[1]
    
    for i in range(int(bottom), int(top)+1):
        ip = ".".join(first_part) + "." + str(i)
        check_ip(ip)
    Last edited by Sharkey; March 31st, 2008 at 09:41 PM.

  3. #3
    Join Date
    Oct 2007
    Posts
    15

    Re: Python Line parser

    Sharkey
    Thank you so much that works great.
    Thanks again for all of the help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured