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


Groups > comp.lang.python > #28968 > unrolled thread

using text file to get ip address from hostname

Started bydkatorza@gmail.com
First post2012-09-12 07:24 -0700
Last post2012-09-19 22:13 -0700
Articles 20 on this page of 22 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  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

Page 1 of 2  [1] 2  Next page →


#28968 — using text file to get ip address from hostname

Fromdkatorza@gmail.com
Date2012-09-12 07:24 -0700
Subjectusing text file to get ip address from hostname
Message-ID<e4e100d1-bdf9-45d0-a137-615b1e820cfb@googlegroups.com>
hello ,

i'm new to Python and i searched the web and could not find an answer for my issue.

i need to get an ip address from list of hostnames which are in a textfile.

this is what i have so far 
--------------------------------------------------------------------------
#!/usr/bin/env python
#Get the IP Address

import socket
hostname = 'need it to read from a text file'
addr = socket.gethostbyname(hostname)
print 'The address of ', hostname, 'is', addr 

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

any idea ? 
sorry for my english

thanks.

[toc] | [next] | [standalone]


#28970

FromChris Angelico <rosuav@gmail.com>
Date2012-09-13 00:35 +1000
Message-ID<mailman.565.1347460533.27098.python-list@python.org>
In reply to#28968
On Thu, Sep 13, 2012 at 12:24 AM,  <dkatorza@gmail.com> wrote:
> i'm new to Python and i searched the web and could not find an answer for my issue.
>
> i need to get an ip address from list of hostnames which are in a textfile.

This is sounding like homework, so I'll just give you a basic pointer.

You have there something that successfully resolves one hostname to an
IP address. Now you want to expand that to reading an entire file of
them and resolving them all. Presumably you need to produce a list of
IP addresses; check the question as to whether you need to create a
file, or output to the screen, or something else.

What you want, here, is to open a file and iterate over it. The most
convenient way would be to have one hostname per line and iterate over
the lines of the file. Check out these pages in the Python docs
(you're using Python 2 so I'm going with Python 2.7.3 documentation):

Opening a file:
http://docs.python.org/library/functions.html#open
Ensuring that it'll be closed when you're done:
http://docs.python.org/reference/compound_stmts.html#the-with-statement
Looping over an iterable:
http://docs.python.org/tutorial/controlflow.html#for-statements

See where that takes you; in fact, all of
http://docs.python.org/tutorial/ is worth reading, if you haven't
already.

Have fun, enjoy Python!

ChrisA

[toc] | [prev] | [next] | [standalone]


#28971

Fromdkatorza@gmail.com
Date2012-09-12 07:41 -0700
Message-ID<b025598a-8593-4830-b4a8-a6f5d8ea238c@googlegroups.com>
In reply to#28968
בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת dkat...@gmail.com:
> hello ,
> 
> 
> 
> i'm new to Python and i searched the web and could not find an answer for my issue.
> 
> 
> 
> i need to get an ip address from list of hostnames which are in a textfile.
> 
> 
> 
> this is what i have so far 
> 
> --------------------------------------------------------------------------
> 
> #!/usr/bin/env python
> 
> #Get the IP Address
> 
> 
> 
> import socket
> 
> hostname = 'need it to read from a text file'
> 
> addr = socket.gethostbyname(hostname)
> 
> print 'The address of ', hostname, 'is', addr 
> 
> 
> 
> ---------------------------------------------------------------------------
> 
> 
> 
> any idea ? 
> 
> sorry for my english
> 
> 
> 
> thanks.

thank you ChrisA
it's not really homework, i found a lab exercise on the web and i;m trying to study with it. maybe not the most efficient way.

i have a file with hostnames ordered line by line.

i will check the sources and will get back with outcome.

once again , Thanks,

[toc] | [prev] | [next] | [standalone]


#28990

FromAlister <alister.ware@ntlworld.com>
Date2012-09-12 17:51 +0000
Message-ID<HY34s.27166$CU7.16573@fx02.am4>
In reply to#28971
On Wed, 12 Sep 2012 07:41:10 -0700, dkatorza wrote:

> בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת dkat...@gmail.com:
>> hello ,
>> 
>> 
>> 
>> i'm new to Python and i searched the web and could not find an answer
>> for my issue.
>> 
>> 
>> 
>> i need to get an ip address from list of hostnames which are in a
>> textfile.
>> 
>> 
>> 
>> this is what i have so far
>> 
>> 
--------------------------------------------------------------------------
>> 
>> #!/usr/bin/env python
>> 
>> #Get the IP Address
>> 
>> 
>> 
>> import socket
>> 
>> hostname = 'need it to read from a text file'
>> 
>> addr = socket.gethostbyname(hostname)
>> 
>> print 'The address of ', hostname, 'is', addr
>> 
>> 
>> 
>> 
---------------------------------------------------------------------------
>> 
>> 
>> 
>> any idea ?
>> 
>> sorry for my english
>> 
>> 
>> 
>> thanks.
> 
> thank you ChrisA it's not really homework, i found a lab exercise on the
> web and i;m trying to study with it. maybe not the most efficient way.
> 
> i have a file with hostnames ordered line by line.
> 
> i will check the sources and will get back with outcome.
> 
> once again , Thanks,

so self inflicted homework :-), a good way to learn so ChrisA's pointers 
of where to look are probably the best thing for you.

if you get stuck post back sample code & i am sure someone will be happy 
to hint a little deeper

Good luck



-- 
QOTD:
	"Wouldn't it be wonderful if real life supported control-Z?"

[toc] | [prev] | [next] | [standalone]


#28999

FromTerry Reedy <tjreedy@udel.edu>
Date2012-09-12 17:04 -0400
Message-ID<mailman.579.1347483927.27098.python-list@python.org>
In reply to#28971
On 9/12/2012 10:41 AM, dkatorza@gmail.com wrote:


> it's not really homework, i found a lab exercise on the web
 > and i;m trying to study with it. maybe not the most efficient way.
>
> i have a file with hostnames ordered line by line.

Key fact for this exercise: open files are iterable.
So your top level structure is...

for line in open(<hostname file>):
   <process hostname in line>

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#29012

FromJason Friedman <jason@powerpull.net>
Date2012-09-12 21:12 -0600
Message-ID<mailman.591.1347505975.27098.python-list@python.org>
In reply to#28968
> i need to get an ip address from list of hostnames which are in a textfile.
>
> this is what i have so far
> --------------------------------------------------------------------------
> #!/usr/bin/env python
> #Get the IP Address
>
> import socket
> hostname = 'need it to read from a text file'
> addr = socket.gethostbyname(hostname)
> print 'The address of ', hostname, 'is', addr

$ cat hostnames
google.com
microsoft.com
facebook.com

$ python3
Python 3.2.3 (default, May  3 2012, 15:51:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> for line in open("hostnames"):
...     hostname = line.strip()
...     print("IP address for {0} is {1}.".format(hostname,
socket.gethostbyname(hostname)))
...
IP address for google.com is 74.125.225.33.
IP address for microsoft.com is 64.4.11.37.
IP address for facebook.com is 69.171.237.16.

[toc] | [prev] | [next] | [standalone]


#29250

FromDan Katorza <dkatorza@gmail.com>
Date2012-09-15 09:20 -0700
Message-ID<0c93c99d-c837-4b20-b78f-8c367ad3cdae@googlegroups.com>
In reply to#28968
בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת Dan Katorza:
> hello ,
> 
> 
> 
> i'm new to Python and i searched the web and could not find an answer for my issue.
> 
> 
> 
> i need to get an ip address from list of hostnames which are in a textfile.
> 
> 
> 
> this is what i have so far 
> 
> --------------------------------------------------------------------------
> 
> #!/usr/bin/env python
> 
> #Get the IP Address
> 
> 
> 
> import socket
> 
> hostname = 'need it to read from a text file'
> 
> addr = socket.gethostbyname(hostname)
> 
> print 'The address of ', hostname, 'is', addr 
> 
> 
> 
> ---------------------------------------------------------------------------
> 
> 
> 
> any idea ? 
> 
> sorry for my english
> 
> 
> 
> thanks.

hello again friends,
thanks for everyone help on this.
i guess i figured it out in two ways.
the second one i prefer the most.

i will appreciate if someone can give me some tips.
thanks again 

so...
-------------------------------------------------------------
First 
-------------------------------------------------------------
#!/usr/bin/env python
#Get the IP Address


print("hello, please enter file name here >"),
import socket
for line in open(raw_input()):
    hostname = line.strip()
    print("IP address for {0} is {1}.".format(hostname,socket.gethostbyname(hostname)))

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

import os

print("Hello, please enter file name here >"),
FILENAME = raw_input()
if os.path.isfile(FILENAME):
    print("\nFile Exist!")
    print("\nGetting ip from host name")
    print("\n")
    import socket
    for line in open (FILENAME):
        hostname = line.strip()
        print("IP address for {0} is {1}.".format(hostname,socket.gethostbyname(hostname)))
    else:
        print ("\nFinished the operation")
else:
    print ("\nFIle is missing or is not reasable"),
~                                                           

[toc] | [prev] | [next] | [standalone]


#29270

FromHans Mulder <hansmu@xs4all.nl>
Date2012-09-15 20:52 +0200
Message-ID<5054ce77$0$6987$e4fe514c@news2.news.xs4all.nl>
In reply to#29250
On 15/09/12 18:20:42, Dan Katorza wrote:
> בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת Dan Katorza:
>> hello ,
>>
>>
>>
>> i'm new to Python and i searched the web and could not find an answer for my issue.
>>
>>
>>
>> i need to get an ip address from list of hostnames which are in a textfile.
>>
>>
>>
>> this is what i have so far 
>>
>> --------------------------------------------------------------------------
>>
>> #!/usr/bin/env python
>>
>> #Get the IP Address
>>
>>
>>
>> import socket
>>
>> hostname = 'need it to read from a text file'
>>
>> addr = socket.gethostbyname(hostname)
>>
>> print 'The address of ', hostname, 'is', addr 
>>
>>
>>
>> ---------------------------------------------------------------------------
>>
>>
>>
>> any idea ? 
>>
>> sorry for my english
>>
>>
>>
>> thanks.
> 
> hello again friends,
> thanks for everyone help on this.
> i guess i figured it out in two ways.
> the second one i prefer the most.
> 
> i will appreciate if someone can give me some tips.
> thanks again 
> 
> so...
> -------------------------------------------------------------
> First 
> -------------------------------------------------------------
> #!/usr/bin/env python
> #Get the IP Address
> 
> 
> print("hello, please enter file name here >"),

Instead of printing this string, you can pass it as the
argument to raw_input:

for line in open(raw_input("hello, please enter file name here> ")):

Cosmetically, I'd prefer a space after the '>'.

> import socket

PEP 8 recommends putting all imports at the top of the file, like you
do in your second script.

> for line in open(raw_input()):
>     hostname = line.strip()
>     print("IP address for {0} is {1}.".format(hostname,socket.gethostbyname(hostname)))

To my mind, this line does two things: it finds the IP address and
prints it.  I think it would be more readable to do these on separate
lines:

    ip_address = socket.gethostbyname(hostname)
    print("IP address for {0} is {1}.".format(hostname, ip_address)

This forces you to find a good name for the value returned
by gethostbyname, which I consider a Good Thing (tm).

PEP 8 recommends that lines should not be longer than 80
characters.  Your line is longer than that; splitting it
in two conceptual steps nicely solves that issue as well.

> ------------------------------------------------------------
> second
> ------------------------------------------------------------
> #!/usr/bin/env python
> #Get the IP Address
> 
> import os
> 
> print("Hello, please enter file name here >"),
> FILENAME = raw_input()

PEP 8 recommends all upper case for constants, for example
socket.IPPROTO_IPV6.  The filename here is not a hard-wired
constant, so it should be in lower case.

> if os.path.isfile(FILENAME):

Your first script allowed me to enter "/dev/tty" at the prompt,
and then type a bunch of hostnames and an end-of-file character.
This script reports "FIle is missing or not reasable", because
/dev/tty is not a regular file.  I think the message should be
"File is missing or not readable or not a regular file".

I'm always annoyed when I get an error message with several
"or"s in it.  I prefer programs that figure out which of the
three potential issues is the case, and mention only one cause.

>     print("\nFile Exist!")
>     print("\nGetting ip from host name")
>     print("\n")
>     import socket
>     for line in open (FILENAME):
>         hostname = line.strip()
>         print("IP address for {0} is {1}.".format(hostname,socket.gethostbyname(hostname)))
>     else:
>         print ("\nFinished the operation")
> else:
>     print ("\nFIle is missing or is not reasable"),

You don't want a comma at the end of this line: it messes
up the next shell prompt.

Also, this line a rather far away from the test that triggers it.

How about:

filename = raw_input("Hello, please enter file name here> ")
if not os.path.isfile(filename):
    if not os.exist(filename):
	print("\nFile {} does not exist")
    else:
	print("\nFile {} is not a regular file")
    sys.exit(1)

print("\nFile {} exists", filename)
# etc.

Or you could skip the whole 'os' thing and use a try/except
construct instead:

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

import sys, socket

filename = raw_input("Hello, please enter file name here> ")
try:
    infile = open(filename)
except EnvironmentError as e:
    print(e)
    sys.exit(1)

print("\nFile {} exists!".format(filename))
print("\nGetting IP addresses for hosts")
print("\n")
for line in infile:
    hostname = line.strip()
    try:
	ip_address = socket.gethostbyname(hostname)
    except EnvironmentError as e:
	print("Couldn't find IP address for {}: {}".format(hostname, e))
	continue
    print("IP address for {0} is {1}.".format(hostname, ip_address))
else:
    print ("\nFinished the operation")


Using try/except has the advantage that it will correctly
report issues you didn't think about (for example, if file
exists, but you don't have permission to read it).


Hope this helps

-- HansM


[toc] | [prev] | [next] | [standalone]


#29387

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2012-09-17 22:37 +0200
Message-ID<k381lp$fut$1@r03.glglgl.gl>
In reply to#29250
Am 15.09.2012 18:20 schrieb Dan Katorza:

> hello again friends,
> thanks for everyone help on this.
> i guess i figured it out in two ways.
> the second one i prefer the most.
>
> i will appreciate if someone can give me some tips.
> thanks again
>
> so...
> -------------------------------------------------------------
> First
> -------------------------------------------------------------
> #!/usr/bin/env python
> #Get the IP Address
>
>
> print("hello, please enter file name here>"),
> import socket
> for line in open(raw_input()):
>      hostname = line.strip()
>      print("IP address for {0} is {1}.".format(hostname,socket.gethostbyname(hostname)))
>
> ------------------------------------------------------------
> second
> ------------------------------------------------------------
> #!/usr/bin/env python
> #Get the IP Address
>
> import os
>
> print("Hello, please enter file name here>"),
> FILENAME = raw_input()
> if os.path.isfile(FILENAME):
>      print("\nFile Exist!")
>      print("\nGetting ip from host name")
>      print("\n")
>      import socket
>      for line in open (FILENAME):
>          hostname = line.strip()
>          print("IP address for {0} is {1}.".format(hostname,socket.gethostbyname(hostname)))
>      else:
>          print ("\nFinished the operation")
> else:
>      print ("\nFIle is missing or is not reasable"),
> ~

Comparing these, the first one wins if you catch and process exceptions. 
It is easier to ask for forgiveness than to get permission (EAFP, 
http://en.wikipedia.org/wiki/EAFP).

Bit I wonder that no one has mentionned that 
socket.gethostbyname(hostname) is quite old-age because it only returns 
IPv4 addresses (resp. only one of them).

OTOH, socket.getaddrinfo(hostname, 0, 0, socket.SOCK_STREAM) gives you a 
list of parameter tuples for connecting.

So which way you go above, you should change the respective lines to

for line in ...:
     hostname = line.strip()
     for target in socket.getaddrinfo(hostname, 0, socket.AF_UNSPEC,
             socket.SOCK_STREAM):
         print("IP address for {0} is {1}.".format(hostname,
             target[4][0]))


Thomas

[toc] | [prev] | [next] | [standalone]


#29276

FromDan Katorza <dkatorza@gmail.com>
Date2012-09-15 15:43 -0700
Message-ID<3359a0e7-f7bb-4b1f-b586-e83f337566b7@googlegroups.com>
In reply to#28968
בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת Dan Katorza:
> hello ,
> 
> 
> 
> i'm new to Python and i searched the web and could not find an answer for my issue.
> 
> 
> 
> i need to get an ip address from list of hostnames which are in a textfile.
> 
> 
> 
> this is what i have so far 
> 
> --------------------------------------------------------------------------
> 
> #!/usr/bin/env python
> 
> #Get the IP Address
> 
> 
> 
> import socket
> 
> hostname = 'need it to read from a text file'
> 
> addr = socket.gethostbyname(hostname)
> 
> print 'The address of ', hostname, 'is', addr 
> 
> 
> 
> ---------------------------------------------------------------------------
> 
> 
> 
> any idea ? 
> 
> sorry for my english
> 
> 
> 
> thanks.

Hi Hans,
thank you very much for the tips.
as i mentioned before I'm new to python and I'm trying to learn it step by step.
thank you for showing me other ways, i will explore them.

[toc] | [prev] | [next] | [standalone]


#29468

FromDan Katorza <dkatorza@gmail.com>
Date2012-09-19 00:41 -0700
Message-ID<b59ef403-5b4b-49a0-a3a8-1d2321263ff0@googlegroups.com>
In reply to#29276
בתאריך יום ראשון, 16 בספטמבר 2012 01:43:31 UTC+3, מאת Dan Katorza:
> בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת Dan Katorza:
> 
> > hello ,
> 
> > 
> 
> > 
> 
> > 
> 
> > i'm new to Python and i searched the web and could not find an answer for my issue.
> 
> > 
> 
> > 
> 
> > 
> 
> > i need to get an ip address from list of hostnames which are in a textfile.
> 
> > 
> 
> > 
> 
> > 
> 
> > this is what i have so far 
> 
> > 
> 
> > --------------------------------------------------------------------------
> 
> > 
> 
> > #!/usr/bin/env python
> 
> > 
> 
> > #Get the IP Address
> 
> > 
> 
> > 
> 
> > 
> 
> > import socket
> 
> > 
> 
> > hostname = 'need it to read from a text file'
> 
> > 
> 
> > addr = socket.gethostbyname(hostname)
> 
> > 
> 
> > print 'The address of ', hostname, 'is', addr 
> 
> > 
> 
> > 
> 
> > 
> 
> > ---------------------------------------------------------------------------
> 
> > 
> 
> > 
> 
> > 
> 
> > any idea ? 
> 
> > 
> 
> > sorry for my english
> 
> > 
> 
> > 
> 
> > 
> 
> > thanks.
> 
> 
> 
> Hi Hans,
> 
> thank you very much for the tips.
> 
> as i mentioned before I'm new to python and I'm trying to learn it step by step.
> 
> thank you for showing me other ways, i will explore them.

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 :)

[toc] | [prev] | [next] | [standalone]


#29470

FromChris Angelico <rosuav@gmail.com>
Date2012-09-19 18:14 +1000
Message-ID<mailman.902.1348042468.27098.python-list@python.org>
In reply to#29468
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

[toc] | [prev] | [next] | [standalone]


#29471

FromDan Katorza <dkatorza@gmail.com>
Date2012-09-19 01:50 -0700
Message-ID<d50c6d5c-f918-4684-a92b-257c806eec94@googlegroups.com>
In reply to#29470
בתאריך יום רביעי, 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.

[toc] | [prev] | [next] | [standalone]


#29473

FromChris Angelico <rosuav@gmail.com>
Date2012-09-19 18:59 +1000
Message-ID<mailman.904.1348045163.27098.python-list@python.org>
In reply to#29471
On Wed, Sep 19, 2012 at 6:50 PM, Dan Katorza <dkatorza@gmail.com> wrote:
> i know about the while loop , but forgive me i just don't have a clue how to use it for this situation.

You've already used one. What you need to do is surround your entire
code with the loop, so that as soon as it gets to the bottom, it goes
back to the top.

Tip: You'll be indenting the bulk of your code.

ChrisA

[toc] | [prev] | [next] | [standalone]


#29472

FromDan Katorza <dkatorza@gmail.com>
Date2012-09-19 01:50 -0700
Message-ID<mailman.903.1348044655.27098.python-list@python.org>
In reply to#29470
בתאריך יום רביעי, 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.

[toc] | [prev] | [next] | [standalone]


#29474

FromDan Katorza <dkatorza@gmail.com>
Date2012-09-19 02:10 -0700
Message-ID<f0043df6-28c9-4256-9cfa-1e86d9635934@googlegroups.com>
In reply to#29472
בתאריך יום רביעי, 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.

[toc] | [prev] | [next] | [standalone]


#29475

FromDan Katorza <dkatorza@gmail.com>
Date2012-09-19 02:10 -0700
Message-ID<mailman.905.1348045864.27098.python-list@python.org>
In reply to#29472
בתאריך יום רביעי, 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.

[toc] | [prev] | [next] | [standalone]


#29487

FromDan Katorza <dkatorza@gmail.com>
Date2012-09-19 05:28 -0700
Message-ID<1ac7b836-abfb-401b-b6ac-3ae4ad6c1ce7@googlegroups.com>
In reply to#29475
בתאריך יום רביעי, 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()



[toc] | [prev] | [next] | [standalone]


#29514

FromDave Angel <d@davea.name>
Date2012-09-19 14:22 -0400
Message-ID<mailman.933.1348078992.27098.python-list@python.org>
In reply to#29487
On 09/19/2012 08:28 AM, Dan Katorza wrote:
> בתאריך יום רביעי, 19 בספטמבר 2012 12:11:04 UTC+3, מאת Dan Katorza:
>> <SNIP>
>> hi, ll like
>> found a solution,
>> it's not quite like Chris advised but it works.

Not at all like Chris advised.  But it also doesn't help you understand
programming.  Two concepts you're going to have to get a lot more
comfortable with, in Python, or in some other language.  One is loops,
and the other is functions.
>> #!/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()
>>
>>
>>
>>
This is one enormous top-level code, and when you needed to enclose it
in a loop, your answer is to start a new process!  You also duplicate
quite a few lines, rather than making a function for them, and calling
it from two places.


-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#29488

FromDan Katorza <dkatorza@gmail.com>
Date2012-09-19 05:28 -0700
Message-ID<mailman.914.1348057702.27098.python-list@python.org>
In reply to#29475
בתאריך יום רביעי, 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()



[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web