Posts: 104
Threads: 36
Joined: Oct 2017
Oct-27-2019, 11:54 AM
(This post was last modified: Oct-27-2019, 11:54 AM by evilcode1.)
hello all im trying to code a tool to check the status for all websites in server IP : code :
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import requests as qan
import time
from beautifultable import BeautifulTable
import warnings
warnings.filterwarnings('ignore', message='Unverified HTTPS request')
table = BeautifulTable()
def readfile():
with open('/root/domain.txt') as fp:
for line in fp:
http = "http://" + line.rstrip()
u = qan.get(http ,verify= False)
u = (str(u.status_code))
table.column_headers =["Website", "Status", "Time"]
table.column_alignments['Website'] = BeautifulTable.ALIGN_LEFT
table.set_style(BeautifulTable.STYLE_MARKDOWN)
table.append_row([http, u, str(time.clock()) ])
print(table)
#print(http + " \t\t\t== > Status : " + u + " \t took : " + str(time.clock()) )
readfile() when i runt it i got this result :
its print the table header multi times i need to print it just one time how i can do that ??
Posts: 212
Threads: 25
Joined: Aug 2019
Oct-27-2019, 12:47 PM
(This post was last modified: Oct-27-2019, 03:46 PM by newbieAuggie2019.)
(Oct-27-2019, 11:54 AM)evilcode1 Wrote: def readfile():
with open('/root/domain.txt') as fp:
for line in fp:
http = "http://" + line.rstrip()
u = qan.get(http ,verify= False)
u = (str(u.status_code))
table.column_headers =["Website", "Status", "Time"] its print the table header multi times i need to print it just one time how i can do that ??
Hi!
It's just a thought, but it seems that you have put the table header inside the loop, so every time it reads a line in fp, it would print the header. Have you tried leaving the header line out of the loop and at the same level of indentation as for line in fp ?:
def readfile():
with open('/root/domain.txt') as fp:
table.column_headers =["Website", "Status", "Time"]
for line in fp:
http = "http://" + line.rstrip()
u = qan.get(http ,verify= False)
u = (str(u.status_code)) All the best,
I HAVE JUST MADE A DUMMY TEST, WITH SOME MODIFICATIONS OF YOUR CODE TO BE ABLE TO RUN IT ON MY COMPUTER (SO I JUST COMMENTED OUT SOME LINES OR MADE SOME OTHER ADJUSTMENTS, BUT THAT CAN BE EASILY REVERTED AND APPLIED TO YOUR PROGRAM):
import sys
##reload(sys)
##sys.setdefaultencoding('utf8')
import requests as qan
import time
from beautifultable import BeautifulTable
import warnings
warnings.filterwarnings('ignore', message='Unverified HTTPS request')
table = BeautifulTable()
def readfile():
with open('C:/sites_list_01.txt') as fp:
table.column_headers =["Website", "Status", "Time"]
table.column_alignments['Website'] = BeautifulTable.ALIGN_LEFT
table.set_style(BeautifulTable.STYLE_MARKDOWN)
for line in fp:
table.append_row(['http://XXXXXXXXX', '200', str(time.clock()) ])
## http = "http://" + line.rstrip()
## u = qan.get(http ,verify= False)
## u = (str(u.status_code))
## print(http + " \t\t\t== > Status : " + u + " \t took : " + str(time.clock()) )
print(table)
readfile() where 'C:/sites_list_01.txt' is a dummy text file with just 11 sites that I used to test the program (I have already eliminated it), but the program with these modifications to run on my computer produced the following output (just one header row):
Output: | Website | Status | Time |
|------------------|--------|-------|
| http://XXXXXXXXX | 200 | 0.632 |
| http://XXXXXXXXX | 200 | 0.632 |
| http://XXXXXXXXX | 200 | 0.632 |
| http://XXXXXXXXX | 200 | 0.632 |
| http://XXXXXXXXX | 200 | 0.632 |
| http://XXXXXXXXX | 200 | 0.632 |
| http://XXXXXXXXX | 200 | 0.632 |
| http://XXXXXXXXX | 200 | 0.632 |
| http://XXXXXXXXX | 200 | 0.632 |
| http://XXXXXXXXX | 200 | 0.632 |
| http://XXXXXXXXX | 200 | 0.632 |
>>>
So I think you could adapt it to your desired output.
All the best,
I think I should also point out that I received a deprecation warning message:
Error: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead.
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Posts: 104
Threads: 36
Joined: Oct 2017
Oct-29-2019, 08:06 AM
(This post was last modified: Oct-29-2019, 08:06 AM by evilcode1.)
thank u brother for replay and for trying to help but unfortunately does not work i test it on multi machines with py3 and py2 i dont know what is the problem :( i will send u my domain list on inbox check it and try to help me : my final code :
# encoding=utf8
import sys
#reload(sys)
#sys.setdefaultencoding('utf8')
import requests as qan
import time
from beautifultable import BeautifulTable
import warnings
from datetime import datetime
now = datetime.now() # current date and time
from timeit import default_timer as timer
warnings.filterwarnings('ignore', message='Unverified HTTPS request')
table = BeautifulTable()
def readfile():
with open('/root/domain.txt') as fp:
for line in fp:
try:
start = timer()
http = "http://" + line.rstrip()
u = qan.get(http ,verify= False)
u = (str(u.status_code))
table.column_headers =["Website", "Status", "Time S/M "]
table.column_alignments['Website'] = BeautifulTable.ALIGN_LEFT
table.set_style(BeautifulTable.STYLE_MARKDOWN)
end = timer()
table.append_row([http, u, str(end - start) ])
print(table)
#print(http + " \t\t\t== > Status : " + u + " \t took : " + str(time.clock()) )
continue
except qan.exceptions.HTTPError as errh:
continue
except qan.exceptions.ConnectionError as errc:
continue
except qan.exceptions.Timeout as errt:
continue
except qan.exceptions.RequestException as err:
continue
readfile() Output: | Website | Status | Time S/M |
|---------------------|--------|-----------|
| http://xxxxx.com.ni | 200 | 1.008 |
| Website | Status | Time S/M |
|--------------------------------|--------|-----------|
| http://xxxxx.com.ni | 200 | 1.008 |
| http://xxxxxxxxxxxxxxxx.com.ni | 200 | 0.82 |
| Website | Status | Time S/M |
|--------------------------------|--------|-----------|
| http://xxxxx.com.ni | 200 | 1.008 |
| http://xxxxxxxxxxxxxxxx.com.ni | 200 | 0.82 |
| http://xxxxxxx.com.ni | 200 | 0.516 |
| Website | Status | Time S/M |
|--------------------------------|--------|-----------|
| http://xxxxx.com.ni | 200 | 1.008 |
| http://xxxxxxxxxxxxxxxx.com.ni | 200 | 0.82 |
| http://xxxxxxx.com.ni | 200 | 0.516 |
| http://xxxxxxxxx.com.ni | 200 | 0.538 |
Posts: 212
Threads: 25
Joined: Aug 2019
Oct-29-2019, 11:00 AM
(This post was last modified: Oct-29-2019, 11:00 AM by newbieAuggie2019.)
Hi again!
My understanding, seeing your first post, is that your code runs properly, but it prints the header row every time it reads for line in fp because you made the program to print the headers inside the loop for every line read in fp with this part of your code:
def readfile():
with open('/root/domain.txt') as fp:
for line in fp:
http = "http://" + line.rstrip()
u = qan.get(http ,verify= False)
u = (str(u.status_code))
table.column_headers =["Website", "Status", "Time"]
table.column_alignments['Website'] = BeautifulTable.ALIGN_LEFT
table.set_style(BeautifulTable.STYLE_MARKDOWN)
table.append_row([http, u, str(time.clock()) ]) I think that if the image you posted corresponds to that first program, then you should try to leave out of the loop, just the part establishing the unchangeable characteristics to set the table.
If the program that you posted on your first post was running fine, and the only thing not wanted was that the header line was printed more than once, then I would suggest you try this one:
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import requests as qan
import time
from beautifultable import BeautifulTable
import warnings
warnings.filterwarnings('ignore', message='Unverified HTTPS request')
table = BeautifulTable()
def readfile():
with open('/root/domain.txt') as fp:
table.column_headers =["Website", "Status", "Time"]
table.column_alignments['Website'] = BeautifulTable.ALIGN_LEFT
table.set_style(BeautifulTable.STYLE_MARKDOWN)
for line in fp:
http = "http://" + line.rstrip()
u = qan.get(http ,verify= False)
u = (str(u.status_code))
table.append_row([http, u, str(time.clock()) ])
print(table) All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Posts: 104
Threads: 36
Joined: Oct 2017
the same problem brother :( sorry
Posts: 8,090
Threads: 154
Joined: Sep 2016
Oct-29-2019, 11:23 AM
(This post was last modified: Oct-29-2019, 11:23 AM by buran.)
I think line 23 must be outside the loop. At the moment you print the table in every iteration, just adding one more line.
newbieAuggie2019 showed it in his first post already
Posts: 212
Threads: 25
Joined: Aug 2019
(Oct-29-2019, 11:20 AM)buran Wrote: I think line 23 must be outside the loop. At the moment you print the table in every iteration, just adding one more line Oops! I think Buran is right, otherwise a table would be printed again, with the header row again. Sorry for not seeing it myself.
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Posts: 8,090
Threads: 154
Joined: Sep 2016
Oct-29-2019, 11:28 AM
(This post was last modified: Oct-29-2019, 11:28 AM by buran.)
(Oct-29-2019, 11:23 AM)newbieAuggie2019 Wrote: Sorry for not seeing it myself. Well, as I said - you did it in your first post - your line 25 is outside the loop body :-) They didn't implement it
Posts: 104
Threads: 36
Joined: Oct 2017
which line ??
table.column_headers =["Website", "Status", "Time"]
Posts: 8,090
Threads: 154
Joined: Sep 2016
the line you refer is already outside the loop (line 15 in your latests code)
(Oct-29-2019, 11:23 AM)buran Wrote: line 23 must be outside the loop. that is print(table)
|