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!newsfeed6.news.xs4all.nl!xs4all!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; 'received:209.85.223': 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; 'anyway': 0.11; 'language': 0.14; 'finished': 0.15; '(another': 0.16; '6th': 0.16; 'chris,': 0.16; 'code?': 0.16; 'e))': 0.16; 'programing': 0.16; 'situation.': 0.16; 'skip:( 60': 0.16; 'sys.exit(1)': 0.16; 'tutoring': 0.16; '{0}': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'skip:{ 20': 0.17; 'code.': 0.20; '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; 'ago': 0.27; 'guess': 0.27; 'skip:# 10': 0.27; 'question': 0.27; 'forgive': 0.27; 'options': 0.27; 'chris': 0.28; 'ahead.': 0.29; 'dan': 0.29; 'giant': 0.29; 'socket,': 0.29; 'probably': 0.29; "i'm": 0.29; 'code': 0.31; 'file': 0.32; 'addresses': 0.32; 'print': 0.32; 'says': 0.33; 'skip:s 30': 0.33; 'docs': 0.33; 'everyone.': 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; 'thanks': 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; 'too': 0.36; 'bad': 0.37; '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; 'days': 0.60; 'address': 0.60; 'stay': 0.61; 'back': 0.62; 'ever': 0.63; 'show': 0.63; 'here': 0.65; 'decided': 0.65; 'choose': 0.65; 'search,': 0.65; 'skip:\xd7 10': 0.69; '8bit%:100': 0.70; 'realized': 0.71; 'sounds': 0.71; 'day': 0.73; 'subject:get': 0.81; "'while'": 0.84; 'start.': 0.84; 'comment.': 0.91; 'task,': 0.91 Newsgroups: comp.lang.python Date: Wed, 19 Sep 2012 22:13:29 -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: 1026 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348118020 news.xs4all.nl 6907 [2001:888:2000:d::a6]:56129 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29542 =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 15:28:23 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 12:11:0= 4 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:50:5= 6 UTC+3, =D7=9E=D7=90=D7=AA Dan Katorza: >=20 > >=20 >=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:1= 4:29 UTC+3, =D7=9E=D7=90=D7=AA Chris Angelico: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > On Wed, Sep 19, 2012 at 5:41 PM, Dan Katorza w= rote: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > > >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > > Hello again, >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > >=20 >=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 > > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > > Is there any option where you can set the program to go back to l= ets say the top of the code? >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > > I mean if the program finished the operation and i want to stay i= n the program and go back ro the start. >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > > 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. >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > > >=20 > >=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 > > > >=20 >=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 > > > >=20 >=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 > > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > > ChrisA >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > Hi Chris, >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > this is my code: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > #!/usr/bin/env python >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > #Get the IP Address >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > import sys, socket >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print ("\n\n#########################################################= ") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print ("# Get IP from Host v 1.0 #") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print ("#########################################################") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print ("# Choose from the options below #") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print ("# 1- url , 2-File(Text file only.txt) #") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print ("#########################################################\n") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > mchoice =3D int(raw_input("Please enter your choice> ")) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > while mchoice !=3D1 and mchoice !=3D2: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print("{0} is not a menu option.".format(mchoice)) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > mchoice =3D int(raw_input("Please try again> ")) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > if mchoice =3D=3D 2: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > filename =3D raw_input("Hello, please enter file name here> ") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > if filename.endswith(".txt"): >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > try: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > infile =3D open(filename) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > except EnvironmentError as e: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print(e) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > sys.exit(1) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print("\nFile {0} exists!".format(filename)) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print("\nGetting IP addresses for hosts") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print("\n") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > else: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print("{0} is not a Text file.".format(filename)) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > sys.exit(1) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > for line in infile: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > hostname =3D line.strip() >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > try: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > ip_address =3D socket.gethostbyname(hostname) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > except EnvironmentError as e: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print("Couldn't find IP address for {0}: {1}".format(hostname= , e)) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > continue >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print("IP address for {0} is {1}.".format(hostname, ip_address)) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > else: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print ("\nFinished the operation") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > if mchoice =3D=3D 1: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > murl =3D raw_input("Enter URL here> ") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > try: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print("Checking URL...") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > ip_address =3D socket.gethostbyname(murl) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > except EnvironmentError as d: >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print(d) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > sys.exit(1) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print("Valid URL") >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print("\nIP address for {0} is {1}.".format(murl, ip_address)) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > print ("\nFinished the operation") >=20 > >=20 >=20 > > >=20 >=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 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > now where it says Finsihed the operation i want it to show (another s= earch /main menu/exit program) >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > i know about the while loop , but forgive me i just don't have a clue= how to use it for this situation. >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > i don't want you to give me the code:) just the idea. >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > i did read the section about the while loop but still i do not know h= ow to use it in this situation. >=20 > >=20 >=20 > > >=20 >=20 > >=20 >=20 > > > thanks. >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > o.k a giant while loop :) >=20 > >=20 >=20 > > thanks. >=20 >=20 >=20 > hi,=20 >=20 > found a solution, >=20 > it's not quite like Chris advised but it works. >=20 >=20 >=20 > #!/usr/bin/env python >=20 > #Get the IP Address >=20 >=20 >=20 > import sys, socket, os >=20 >=20 >=20 > def restart_program(): >=20 > python =3D sys.executable >=20 > os.execl(python, python, * sys.argv) >=20 >=20 >=20 > print ("\n\n#########################################################") >=20 > print ("# Get IP from Host v 1.0 #") >=20 > print ("#########################################################") >=20 > print ("# Choose from the options below #") >=20 > print ("# 1- url , 2-File(Text file only.txt) #") >=20 > print ("#########################################################\n") >=20 >=20 >=20 > mchoice =3D int(raw_input("Please enter your choice> ")) >=20 > while mchoice !=3D1 and mchoice !=3D2: >=20 > print("{0} is not a menu option.".format(mchoice)) >=20 > mchoice =3D int(raw_input("Please try again> ")) >=20 >=20 >=20 >=20 >=20 > while mchoice =3D=3D 2: >=20 > filename =3D raw_input("Please enter file name here> ") >=20 > if filename.endswith(".txt"): >=20 >=20 >=20 > try: >=20 > infile =3D open(filename) >=20 > except EnvironmentError as e: >=20 > print(e) >=20 > sys.exit(1) >=20 >=20 >=20 > print("\nFile {0} exists!".format(filename)) >=20 > print("\nGetting IP addresses for hosts") >=20 > print("\n") >=20 > else: >=20 > print("{0} is not a Text file.".format(filename)) >=20 > sys.exit(1) >=20 > for line in infile: >=20 > hostname =3D line.strip() >=20 > try: >=20 > ip_address =3D socket.gethostbyname(hostname) >=20 > except EnvironmentError as e: >=20 > print("Couldn't find IP address for {0}: {1}".format(hostname= , e)) >=20 > continue >=20 > print("IP address for {0} is {1}.".format(hostname, ip_address)) >=20 > else: >=20 > print ("\nFinished the operation") >=20 > print ("A=3Danother search, M=3Dmain menu, E=3Dexit") >=20 >=20 >=20 > waction=3Draw_input("Please choose your action > ") >=20 >=20 >=20 > while waction !=3D'A' and waction !=3D'M' and waction !=3D'E': >=20 > print("{0} is not a valid action.".format(waction)) >=20 > waction=3Draw_input("Please try again> ") >=20 > if waction =3D=3D'E': >=20 > sys.exit(1) >=20 > if waction =3D=3D'A': >=20 > continue >=20 > if waction =3D=3D'M': >=20 > print ("#####################################################= ####") >=20 > print ("# Choose from the options below = #") >=20 > print ("# 1- url , 2-File(Text file only.txt) = #") >=20 > print ("#####################################################= ####\n") >=20 >=20 >=20 > mchoice =3D int(raw_input("Please enter your choice> ")) >=20 > while mchoice !=3D1 and mchoice !=3D2: >=20 > print("{0} is not a menu option.".format(mchoice)) >=20 > mchoice =3D int(raw_input("Please try again> ")) >=20 >=20 >=20 >=20 >=20 > while mchoice =3D=3D 1: >=20 > murl =3D raw_input("Enter URL here> ") >=20 > try: >=20 > print("Checking URL...") >=20 > ip_address =3D socket.gethostbyname(murl) >=20 > except EnvironmentError as d: >=20 > print(d) >=20 > sys.exit(1) >=20 > print("Valid URL") >=20 > print("\nIP address for {0} is {1}.".format(murl, ip_address)) >=20 > print ("\nFinished the operation") >=20 > print ("A=3Danother search, M=3Dmain menu, E=3Dexit") >=20 >=20 >=20 > waction=3Draw_input("Please choose your action > ") >=20 >=20 >=20 > while waction !=3D'A' and waction !=3D'M' and waction !=3D'E': >=20 > print("{0} is not a valid action.".format(waction)) >=20 > waction=3Draw_input("Please try again> ") >=20 > if waction =3D=3D'E': >=20 > sys.exit(1) >=20 > if waction =3D=3D'A': >=20 > continue >=20 > if waction =3D=3D'M': >=20 > restart_program() Hi Dave, thanks for your comment. please note this is only my 6th day of programing in any language ever and = i'm tutoring my self. I know this code is too long and lousy for a simple task, but i guess this = is part of my learning process. 3 days ago i already figured out that is too long and probably there is bet= ter solution, but I decided to try and work it out anyway just for learning= and dealing with a bad code. anyway what i realized is before i want to write a program/task , i need to= write what i want before and plan it ahead. anyway thanks everyone.