Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29488

Re: using text file to get ip address from hostname

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <dkatorza@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'else:': 0.03; 'socket': 0.05; 'subject:text': 0.05; 'exit': 0.07; 'filename': 0.07; 'subject:file': 0.07; 'sys,': 0.07; 'try:': 0.07; 'works.': 0.07; 'python': 0.09; 'hostname': 0.09; 'loop.': 0.09; 'sep': 0.09; 'solution,': 0.09; 'subject:using': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'finished': 0.15; '(another': 0.16; 'chris,': 0.16; 'code?': 0.16; 'e))': 0.16; 'situation.': 0.16; 'skip:( 60': 0.16; 'sys.exit(1)': 0.16; '{0}': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'skip:{ 20': 0.17; 'import': 0.21; 'thanks.': 0.21; 'lets': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'host': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'skip:# 10': 0.27; 'question': 0.27; 'forgive': 0.27; 'options': 0.27; 'chris': 0.28; 'dan': 0.29; 'giant': 0.29; 'socket,': 0.29; "i'm": 0.29; 'file': 0.32; 'addresses': 0.32; 'print': 0.32; 'says': 0.33; 'skip:s 30': 0.33; 'docs': 0.33; 'code:': 0.33; 'tutorial': 0.33; 'hi,': 0.33; 'another': 0.33; 'program,': 0.34; 'received:google.com': 0.34; 'text': 0.34; 'clear': 0.35; 'pm,': 0.35; 'continue': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'skip:{ 10': 0.36; 'option': 0.37; 'quite': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'skip:o 20': 0.38; 'where': 0.40; 'your': 0.60; 'address': 0.60; 'stay': 0.61; 'back': 0.62; 'show': 0.63; 'here': 0.65; 'choose': 0.65; 'search,': 0.65; 'skip:\xd7 10': 0.69; '8bit%:100': 0.70; 'sounds': 0.71; 'subject:get': 0.81; "'while'": 0.84; 'start.': 0.84
Newsgroups comp.lang.python
Date Wed, 19 Sep 2012 05:28:19 -0700 (PDT)
In-Reply-To <mailman.905.1348045864.27098.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=85.65.169.174; posting-account=_vepBgoAAAD3Jx-8VUeoi33AOFj3T9H8
References <e4e100d1-bdf9-45d0-a137-615b1e820cfb@googlegroups.com> <3359a0e7-f7bb-4b1f-b586-e83f337566b7@googlegroups.com> <b59ef403-5b4b-49a0-a3a8-1d2321263ff0@googlegroups.com> <mailman.902.1348042468.27098.python-list@python.org> <mailman.903.1348044655.27098.python-list@python.org> <mailman.905.1348045864.27098.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 85.65.169.174
MIME-Version 1.0
Subject Re: using text file to get ip address from hostname
From Dan Katorza <dkatorza@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.914.1348057702.27098.python-list@python.org> (permalink)
Lines 519
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348057702 news.xs4all.nl 6979 [2001:888:2000:d::a6]:34614
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29488

Show key headers only | View raw


בתאריך יום רביעי, 19 בספטמבר 2012 12:11:04 UTC+3, מאת Dan Katorza:
> בתאריך יום רביעי, 19 בספטמבר 2012 11:50:56 UTC+3, מאת Dan Katorza:
> 
> > בתאריך יום רביעי, 19 בספטמבר 2012 11:14:29 UTC+3, מאת Chris Angelico:
> 
> > 
> 
> > > On Wed, Sep 19, 2012 at 5:41 PM, Dan Katorza <dkatorza@gmail.com> wrote:
> 
> > 
> 
> > > 
> 
> > 
> 
> > > >
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > Hello again,
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > I have another question and i hope you will understand me..
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > Is there any option where you can set the program to go back to lets say the top of the code?
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > I mean if the program finished the operation and i want to stay in the program and go back ro the start.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > after any operation i want the option to do it again , go back to the main menu or full exit from the program, and i want it every time.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > >
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > i hope i'm clear :)
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > Yep! Look up the docs and tutorial on "control flow" and "looping
> 
> > 
> 
> > > 
> 
> > 
> 
> > > constructs". Sounds like what you want here is a 'while' loop.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > ChrisA
> 
> > 
> 
> > 
> 
> > 
> 
> > Hi Chris,
> 
> > 
> 
> > this is my code:
> 
> > 
> 
> > 
> 
> > 
> 
> > #!/usr/bin/env python
> 
> > 
> 
> > #Get the IP Address
> 
> > 
> 
> > 
> 
> > 
> 
> > import sys, socket
> 
> > 
> 
> > 
> 
> > 
> 
> > print ("\n\n#########################################################")
> 
> > 
> 
> > print ("#                Get IP from Host v 1.0                 #")
> 
> > 
> 
> > print ("#########################################################")
> 
> > 
> 
> > print ("#             Choose from the options below             #")
> 
> > 
> 
> > print ("#          1- url , 2-File(Text file only.txt)          #")
> 
> > 
> 
> > print ("#########################################################\n")
> 
> > 
> 
> > 
> 
> > 
> 
> > mchoice = int(raw_input("Please enter your choice> "))
> 
> > 
> 
> > while mchoice !=1 and  mchoice !=2:
> 
> > 
> 
> >     print("{0} is not a menu option.".format(mchoice))
> 
> > 
> 
> >     mchoice = int(raw_input("Please try again> "))
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > if mchoice == 2:
> 
> > 
> 
> >   filename = raw_input("Hello, please enter file name here> ")
> 
> > 
> 
> >   if filename.endswith(".txt"):
> 
> > 
> 
> > 
> 
> > 
> 
> >    try:
> 
> > 
> 
> >     infile = open(filename)
> 
> > 
> 
> >    except EnvironmentError as e:
> 
> > 
> 
> >     print(e)
> 
> > 
> 
> >     sys.exit(1)
> 
> > 
> 
> > 
> 
> > 
> 
> >    print("\nFile {0} exists!".format(filename))
> 
> > 
> 
> >    print("\nGetting IP addresses for hosts")
> 
> > 
> 
> >    print("\n")
> 
> > 
> 
> >   else:
> 
> > 
> 
> >        print("{0} is not a Text file.".format(filename))
> 
> > 
> 
> >        sys.exit(1)
> 
> > 
> 
> >   for line in infile:
> 
> > 
> 
> >     hostname = line.strip()
> 
> > 
> 
> >     try:
> 
> > 
> 
> >         ip_address = socket.gethostbyname(hostname)
> 
> > 
> 
> >     except EnvironmentError as e:
> 
> > 
> 
> >         print("Couldn't find IP address for {0}: {1}".format(hostname, e))
> 
> > 
> 
> >         continue
> 
> > 
> 
> >     print("IP address for {0} is {1}.".format(hostname, ip_address))
> 
> > 
> 
> >   else:
> 
> > 
> 
> >     print ("\nFinished the operation")
> 
> > 
> 
> > 
> 
> > 
> 
> > if mchoice == 1:
> 
> > 
> 
> >   murl = raw_input("Enter URL here> ")
> 
> > 
> 
> >   try:
> 
> > 
> 
> >       print("Checking URL...")
> 
> > 
> 
> >       ip_address = socket.gethostbyname(murl)
> 
> > 
> 
> >   except EnvironmentError as d:
> 
> > 
> 
> >       print(d)
> 
> > 
> 
> >       sys.exit(1)
> 
> > 
> 
> >   print("Valid URL")
> 
> > 
> 
> >   print("\nIP address for {0} is {1}.".format(murl, ip_address))
> 
> > 
> 
> >   print ("\nFinished the operation")
> 
> > 
> 
> > =====================================================================
> 
> > 
> 
> > 
> 
> > 
> 
> > now where it says Finsihed the operation i want it to show (another search /main menu/exit program)
> 
> > 
> 
> > 
> 
> > 
> 
> > i know about the while loop , but forgive me i just don't have a clue how to use it for this situation.
> 
> > 
> 
> > 
> 
> > 
> 
> > i don't want you to give me the code:) just the idea.
> 
> > 
> 
> > i did read the section about the while loop but still i do not know how to use it in this situation.
> 
> > 
> 
> > thanks.
> 
> 
> 
> o.k a giant while loop :)
> 
> thanks.

hi, 
found a solution,
it's not quite like Chris advised but it works.

#!/usr/bin/env python
#Get the IP Address

import sys, socket, os

def restart_program():
    python = sys.executable
    os.execl(python, python, * sys.argv)

print ("\n\n#########################################################")
print ("#                Get IP from Host v 1.0                 #")
print ("#########################################################")
print ("#             Choose from the options below             #")
print ("#          1- url , 2-File(Text file only.txt)          #")
print ("#########################################################\n")

mchoice = int(raw_input("Please enter your choice> "))
while mchoice !=1 and  mchoice !=2:
    print("{0} is not a menu option.".format(mchoice))
    mchoice = int(raw_input("Please try again> "))


while mchoice == 2:
    filename = raw_input("Please enter file name here> ")
    if filename.endswith(".txt"):

        try:
            infile = open(filename)
        except EnvironmentError as e:
            print(e)
            sys.exit(1)

        print("\nFile {0} exists!".format(filename))
        print("\nGetting IP addresses for hosts")
        print("\n")
    else:
        print("{0} is not a Text file.".format(filename))
        sys.exit(1)
    for line in infile:
        hostname = line.strip()
        try:
            ip_address = socket.gethostbyname(hostname)
        except EnvironmentError as e:
            print("Couldn't find IP address for {0}: {1}".format(hostname, e))
            continue
        print("IP address for {0} is {1}.".format(hostname, ip_address))
    else:
        print ("\nFinished the operation")
        print ("A=another search, M=main menu, E=exit")

        waction=raw_input("Please choose your action > ")

        while waction !='A' and waction !='M' and waction !='E':
            print("{0} is not a valid action.".format(waction))
            waction=raw_input("Please try again> ")
        if waction =='E':
            sys.exit(1)
        if waction =='A':
            continue
        if waction =='M':
            print ("#########################################################")
            print ("#             Choose from the options below             #")
            print ("#          1- url , 2-File(Text file only.txt)          #")
            print ("#########################################################\n")

            mchoice = int(raw_input("Please enter your choice> "))
            while mchoice !=1 and  mchoice !=2:
                print("{0} is not a menu option.".format(mchoice))
                mchoice = int(raw_input("Please try again> "))


while mchoice == 1:
    murl = raw_input("Enter URL here> ")
    try:
        print("Checking URL...")
        ip_address = socket.gethostbyname(murl)
    except EnvironmentError as d:
        print(d)
        sys.exit(1)
    print("Valid URL")
    print("\nIP address for {0} is {1}.".format(murl, ip_address))
    print ("\nFinished the operation")
    print ("A=another search, M=main menu, E=exit")

    waction=raw_input("Please choose your action > ")

    while waction !='A' and waction !='M' and waction !='E':
        print("{0} is not a valid action.".format(waction))
        waction=raw_input("Please try again> ")
    if waction =='E':
        sys.exit(1)
    if waction =='A':
        continue
    if waction =='M':
        restart_program()



Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

using text file to get ip address from hostname dkatorza@gmail.com - 2012-09-12 07:24 -0700
  Re: using text file to get ip address from hostname Chris Angelico <rosuav@gmail.com> - 2012-09-13 00:35 +1000
  Re: using text file to get ip address from hostname dkatorza@gmail.com - 2012-09-12 07:41 -0700
    Re: using text file to get ip address from hostname Alister <alister.ware@ntlworld.com> - 2012-09-12 17:51 +0000
    Re: using text file to get ip address from hostname Terry Reedy <tjreedy@udel.edu> - 2012-09-12 17:04 -0400
  Re: using text file to get ip address from hostname Jason Friedman <jason@powerpull.net> - 2012-09-12 21:12 -0600
  Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-15 09:20 -0700
    Re: using text file to get ip address from hostname Hans Mulder <hansmu@xs4all.nl> - 2012-09-15 20:52 +0200
    Re: using text file to get ip address from hostname Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-17 22:37 +0200
  Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-15 15:43 -0700
    Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 00:41 -0700
      Re: using text file to get ip address from hostname Chris Angelico <rosuav@gmail.com> - 2012-09-19 18:14 +1000
        Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 01:50 -0700
          Re: using text file to get ip address from hostname Chris Angelico <rosuav@gmail.com> - 2012-09-19 18:59 +1000
        Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 01:50 -0700
          Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 02:10 -0700
          Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 02:10 -0700
            Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 05:28 -0700
              Re: using text file to get ip address from hostname Dave Angel <d@davea.name> - 2012-09-19 14:22 -0400
            Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 05:28 -0700
              Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 22:13 -0700
              Re: using text file to get ip address from hostname Dan Katorza <dkatorza@gmail.com> - 2012-09-19 22:13 -0700

csiph-web