Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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; 'lines,': 0.05; 'subject:text': 0.05; 'filename': 0.07; 'subject:file': 0.07; 'sys,': 0.07; 'try:': 0.07; 'works.': 0.07; 'python': 0.09; 'enormous': 0.09; 'hostname': 0.09; 'solution,': 0.09; 'subject:using': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'e))': 0.16; 'places.': 0.16; 'skip:( 60': 0.16; 'sys.exit(1)': 0.16; 'top-level': 0.16; '{0}': 0.16; 'wrote:': 0.17; 'duplicate': 0.17; 'skip:{ 20': 0.17; 'code,': 0.18; 'comfortable': 0.20; 'import': 0.21; '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; 'am,': 0.27; 'language.': 0.27; 'skip:# 10': 0.27; 'functions.': 0.27; 'options': 0.27; "doesn't": 0.28; 'chris': 0.28; 'dan': 0.29; 'loop,': 0.29; 'socket,': 0.29; 'function': 0.30; 'file': 0.32; 'addresses': 0.32; 'print': 0.32; 'skip:s 30': 0.33; 'hi,': 0.33; 'text': 0.34; 'needed': 0.35; 'programming.': 0.35; 'continue': 0.35; 'except': 0.36; 'but': 0.36; 'skip:{ 10': 0.36; 'two': 0.37; 'quite': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'your': 0.60; 'address': 0.60; 'more': 0.63; 'making': 0.64; 'choose': 0.65; 'search,': 0.65; 'header:Reply- To:1': 0.68; 'skip:\xd7 10': 0.69; '8bit%:100': 0.70; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'subject:get': 0.81 Date: Wed, 19 Sep 2012 14:22:39 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: Dan Katorza Subject: Re: using text file to get ip address from hostname References: <3359a0e7-f7bb-4b1f-b586-e83f337566b7@googlegroups.com> <1ac7b836-abfb-401b-b6ac-3ae4ad6c1ce7@googlegroups.com> In-Reply-To: <1ac7b836-abfb-401b-b6ac-3ae4ad6c1ce7@googlegroups.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Provags-ID: V02:K0:P2/TVBK/Dh/dTqRbVRdp2jX/uR+Od0nhs0BuX9CfNSa sjS1cFIUXoA010/BgJY8h5nIDP+0+fI2Gh7q3QSoBhdF/c2200 LQfWb0ep8GxOiNuIBlF0Uw42rChtfb3S5t9MKUlu/9nm1Cuc3u LwwYhywrKXBPqKxNIC77pDeAa5vgDJKpV2W+N7baL0v5c8O3bc prpTl67Jo6KejEvZlQj0g413oc/QllcCDmXHL0ItAyoHjQLjt9 VLFn9oqS8Wy8ZM7m801dbRD1lygYtHh0TgD6upoanK3shmRyts NNsXuEmJ52wU3okH2zHhCXQalFcOl2XJdw6oeE1+moUu6LBXw= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 120 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348078992 news.xs4all.nl 6952 [2001:888:2000:d::a6]:46766 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29514 On 09/19/2012 08:28 AM, Dan Katorza wrote: > בתאריך יום רביעי, 19 בספטמבר 2012 12:11:04 UTC+3, מאת Dan Katorza: >> >> 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