Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion using text file to get ip address from hostname
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dave Angel  
View profile  
 More options Sep 19 2012, 2:23 pm
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Wed, 19 Sep 2012 14:22:39 -0400
Local: Wed, Sep 19 2012 2:22 pm
Subject: Re: using text file to get ip address from hostname
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.