Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29488
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2012-09-19 05:28 -0700 |
| References | (1 earlier) <3359a0e7-f7bb-4b1f-b586-e83f337566b7@googlegroups.com> <b59ef403-5b4b-49a0-a3a8-1d2321263ff0@googlegroups.com> <mailman.902.1348042468.27098.python-list@python.org> <mailman.903.1348044655.27098.python-list@python.org> <mailman.905.1348045864.27098.python-list@python.org> |
| Subject | Re: using text file to get ip address from hostname |
| From | Dan Katorza <dkatorza@gmail.com> |
| Message-ID | <mailman.914.1348057702.27098.python-list@python.org> (permalink) |
בתאריך יום רביעי, 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()
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web