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: 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: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=85.65.169.174; posting-account=_vepBgoAAAD3Jx-8VUeoi33AOFj3T9H8 References: <3359a0e7-f7bb-4b1f-b586-e83f337566b7@googlegroups.com> 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: 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 =D7=91=D7=AA=D7=90=D7=A8=D7=99=D7=9A =D7=99=D7=95=D7=9D =D7=A8=D7=91=D7=99= =D7=A2=D7=99, 19 =D7=91=D7=A1=D7=A4=D7=98=D7=9E=D7=91=D7=A8 2012 12:11:04 U= TC+3, =D7=9E=D7=90=D7=AA Dan Katorza: > =D7=91=D7=AA=D7=90=D7=A8=D7=99=D7=9A =D7=99=D7=95=D7=9D =D7=A8=D7=91=D7= =99=D7=A2=D7=99, 19 =D7=91=D7=A1=D7=A4=D7=98=D7=9E=D7=91=D7=A8 2012 11:50:5= 6 UTC+3, =D7=9E=D7=90=D7=AA Dan Katorza: >=20 > > =D7=91=D7=AA=D7=90=D7=A8=D7=99=D7=9A =D7=99=D7=95=D7=9D =D7=A8=D7=91=D7= =99=D7=A2=D7=99, 19 =D7=91=D7=A1=D7=A4=D7=98=D7=9E=D7=91=D7=A8 2012 11:14:2= 9 UTC+3, =D7=9E=D7=90=D7=AA Chris Angelico: >=20 > >=20 >=20 > > > On Wed, Sep 19, 2012 at 5:41 PM, Dan Katorza wro= te: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > Hello again, >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > I have another question and i hope you will understand me.. >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > Is there any option where you can set the program to go back to let= s say the top of the code? >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > I mean if the program finished the operation and i want to stay in = the program and go back ro the start. >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > after any operation i want the option to do it again , go back to t= he main menu or full exit from the program, and i want it every time. >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > i hope i'm clear :) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > Yep! Look up the docs and tutorial on "control flow" and "looping >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > constructs". Sounds like what you want here is a 'while' loop. >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > ChrisA >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > Hi Chris, >=20 > >=20 >=20 > > this is my code: >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > #!/usr/bin/env python >=20 > >=20 >=20 > > #Get the IP Address >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > import sys, socket >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > print ("\n\n#########################################################") >=20 > >=20 >=20 > > print ("# Get IP from Host v 1.0 #") >=20 > >=20 >=20 > > print ("#########################################################") >=20 > >=20 >=20 > > print ("# Choose from the options below #") >=20 > >=20 >=20 > > print ("# 1- url , 2-File(Text file only.txt) #") >=20 > >=20 >=20 > > print ("#########################################################\n") >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > mchoice =3D int(raw_input("Please enter your choice> ")) >=20 > >=20 >=20 > > while mchoice !=3D1 and mchoice !=3D2: >=20 > >=20 >=20 > > print("{0} is not a menu option.".format(mchoice)) >=20 > >=20 >=20 > > mchoice =3D int(raw_input("Please try again> ")) >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > if mchoice =3D=3D 2: >=20 > >=20 >=20 > > filename =3D raw_input("Hello, please enter file name here> ") >=20 > >=20 >=20 > > if filename.endswith(".txt"): >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > try: >=20 > >=20 >=20 > > infile =3D open(filename) >=20 > >=20 >=20 > > except EnvironmentError as e: >=20 > >=20 >=20 > > print(e) >=20 > >=20 >=20 > > sys.exit(1) >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > print("\nFile {0} exists!".format(filename)) >=20 > >=20 >=20 > > print("\nGetting IP addresses for hosts") >=20 > >=20 >=20 > > print("\n") >=20 > >=20 >=20 > > else: >=20 > >=20 >=20 > > print("{0} is not a Text file.".format(filename)) >=20 > >=20 >=20 > > sys.exit(1) >=20 > >=20 >=20 > > for line in infile: >=20 > >=20 >=20 > > hostname =3D line.strip() >=20 > >=20 >=20 > > try: >=20 > >=20 >=20 > > ip_address =3D socket.gethostbyname(hostname) >=20 > >=20 >=20 > > except EnvironmentError as e: >=20 > >=20 >=20 > > print("Couldn't find IP address for {0}: {1}".format(hostname, = e)) >=20 > >=20 >=20 > > continue >=20 > >=20 >=20 > > print("IP address for {0} is {1}.".format(hostname, ip_address)) >=20 > >=20 >=20 > > else: >=20 > >=20 >=20 > > print ("\nFinished the operation") >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > if mchoice =3D=3D 1: >=20 > >=20 >=20 > > murl =3D raw_input("Enter URL here> ") >=20 > >=20 >=20 > > try: >=20 > >=20 >=20 > > print("Checking URL...") >=20 > >=20 >=20 > > ip_address =3D socket.gethostbyname(murl) >=20 > >=20 >=20 > > except EnvironmentError as d: >=20 > >=20 >=20 > > print(d) >=20 > >=20 >=20 > > sys.exit(1) >=20 > >=20 >=20 > > print("Valid URL") >=20 > >=20 >=20 > > print("\nIP address for {0} is {1}.".format(murl, ip_address)) >=20 > >=20 >=20 > > print ("\nFinished the operation") >=20 > >=20 >=20 > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > now where it says Finsihed the operation i want it to show (another sea= rch /main menu/exit program) >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > i know about the while loop , but forgive me i just don't have a clue h= ow to use it for this situation. >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > i don't want you to give me the code:) just the idea. >=20 > >=20 >=20 > > i did read the section about the while loop but still i do not know how= to use it in this situation. >=20 > >=20 >=20 > > thanks. >=20 >=20 >=20 > o.k a giant while loop :) >=20 > thanks. hi,=20 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 =3D 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 =3D int(raw_input("Please enter your choice> ")) while mchoice !=3D1 and mchoice !=3D2: print("{0} is not a menu option.".format(mchoice)) mchoice =3D int(raw_input("Please try again> ")) while mchoice =3D=3D 2: filename =3D raw_input("Please enter file name here> ") if filename.endswith(".txt"): try: infile =3D 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 =3D line.strip() try: ip_address =3D 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=3Danother search, M=3Dmain menu, E=3Dexit") waction=3Draw_input("Please choose your action > ") while waction !=3D'A' and waction !=3D'M' and waction !=3D'E': print("{0} is not a valid action.".format(waction)) waction=3Draw_input("Please try again> ") if waction =3D=3D'E': sys.exit(1) if waction =3D=3D'A': continue if waction =3D=3D'M': print ("#######################################################= ##") print ("# Choose from the options below = #") print ("# 1- url , 2-File(Text file only.txt) = #") print ("#######################################################= ##\n") mchoice =3D int(raw_input("Please enter your choice> ")) while mchoice !=3D1 and mchoice !=3D2: print("{0} is not a menu option.".format(mchoice)) mchoice =3D int(raw_input("Please try again> ")) while mchoice =3D=3D 1: murl =3D raw_input("Enter URL here> ") try: print("Checking URL...") ip_address =3D 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=3Danother search, M=3Dmain menu, E=3Dexit") waction=3Draw_input("Please choose your action > ") while waction !=3D'A' and waction !=3D'M' and waction !=3D'E': print("{0} is not a valid action.".format(waction)) waction=3Draw_input("Please try again> ") if waction =3D=3D'E': sys.exit(1) if waction =3D=3D'A': continue if waction =3D=3D'M': restart_program()