ping www.google.com
python Pinger.py
@echo off
for /f "delims=" %%a in (pinglist.txt) do ping -n 1 %%a >nul && (echo %%a ok) || (echo %%a failed to respond)
pause
elif enter.lower() == "yes":
This way you can replace your 20+ lines of 'elif's with if enter.lower() in ('y', 'yes'):
// True
else:
// False
Soooooo it's this?Code: [Select]@echo off
for /f "delims=" %%a in (pinglist.txt) do ping -n 1 %%a >nul && (echo %%a ok) || (echo %%a failed to respond)
pause
Hey, I see you are new to programming and probably young so I don't want to discourage you, but this script is pretty much useless.
This being said, you should try to improve this things in your following projects:
- Remove sleeping (time.sleep()). It is annoying af and you can always avoid it.
- Avoid asking for agreement each time a script is run. Include the license with you application and don't mention it in the program itself (or at least don't ask people to confirm it each time).
- Don't hardcode input files. Accept input file with parameter (for example -i "URL_list.txt") or read from stdin (this way users can decide how to pass the URLs - write them, pass them from another app, pass them from a file...)
- Add '#!/usr/bin/env python' as the first line of your script. This tells the terminal to run this script with python interpreter.
print '\033[1;32mSOME MESSAGE\033[1;m'
It would be better to replace these with a function and function call:def print_light_green(str):
# Unless you've seen it before, "033[1;32m" is probably meaningless. What if you want to change the colour, or don't bother colouring output if using Windows?
# We use a function here to communicate what it does through its name and make it easier to change due to being in a single place, not to mention remove repetition and 'noise' in the code.
print '033[1;32m' + str + '\033[1;m'
print_light_green("Hello, world!")
print_light_green("Isn't this much prettier?")
non_blank_count = 0
with open('URL_list.txt') as infp:
for line in infp:
if line.strip():
non_blank_count += 1
fo = open("URL_list.txt", "rw+")
Why are you opening this twice, the second in rw mode?file = open('url_list.txt')
non_blank_lines = 0
for line in file:
if line.strip():
non_blank_lines += 1
file.seek(0, 0) # Go back to beginning of file.
Note how I've given your variables more descriptive names and grouped the blank line calculation code together to show that they're related and doing a specific task, making it easier for a programmer to skim over.line = fo.readline()
print '\033[1;33mPinging: %s \033[1;m' % (line),
for x in range(0, URL):
os.system("ping -c 1 %s" % (line)),
line = fo.readline()
print '\033[1;33mPinging: %s \033[1;m' % (line),
What an odd way to do things. I guess you didn't use for line in file for learning purposes, but why do this in and out of the loop instead of:for x in range(0, non_blank_lines):
line = file.readline()
continue if !file.strip() # Skip if the line is blank :P I think this is valid syntax || Perl has altered my memories.
print_light_green("Pinging: %s' % (line))
os.system("ping -c 1 %s" % (line))
file.close()
I don't know, is this even Python?Batch.
enter = raw_input("Do you agree to terms and conditions of 'The Hacker-Toolkit'? [Y/N]")
print(" ")
if enter == 'Y':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'yes':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'y':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'yes':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'yeS':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'yES':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'yEs':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'YEs':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'Yes':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'YES':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'YeS':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
enter = raw_input("Do you agree to terms and conditions of 'The Hacker-Toolkit'? [Y/N]")
print(" ")
yes = ["y","yes"]
if enter.lower() in yes:
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
enter = raw_input("Do you agree to terms and conditions of 'The Hacker-Toolkit'? [Y/N]")
print(" ")
if enter.lower() is "y" or "yes":
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
Code: (Python) [Select]
enter = raw_input("Do you agree to terms and conditions of 'The Hacker-Toolkit'? [Y/N]")
print(" ")
if enter == 'Y':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'yes':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'y':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'yes':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'yeS':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'yES':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'yEs':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'YEs':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'Yes':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'YES':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
elif enter == 'YeS':
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
This part hurt my eyes so much.
simply convert "enter" to lowercase, and look for it in a list.Code: (Python) [Select]enter = raw_input("Do you agree to terms and conditions of 'The Hacker-Toolkit'? [Y/N]")
print(" ")
yes = ["y","yes"]
if enter.lower() in yes:
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)
Or simplerCode: (Python) [Select]enter = raw_input("Do you agree to terms and conditions of 'The Hacker-Toolkit'? [Y/N]")
print(" ")
if enter.lower() is "y" or "yes":
print '\033[1;32mThank you for using The Hacker-Toolkit.\033[1;m'
time.sleep(1.5)