Ok,
here is my test ping code,
My errors are indentations, no matter what I try
either 4 or 8 indentations, I can't get this to work, each time
I Run the code; it keeps suggesting indentation errors.
Once this section of the code is corrected and actually creates
the log file, I hope to move on to the next section of the entire process.
Reason for the ping test and the variables,
A: To confirm it will query
B: to log a log in txt which I'm having trouble with the txt files further down the script.
C: why this web page ? Because it's something to identify with.
note: Run Error message is below the script
#Here's the Python code for the ping test:
import subprocess
import sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])
import requests
import os
import datetime
import time
import requests
# Adjustable variables
num_pings = 7
interval = 1 # seconds
# URL to ping
url = "
https://tab.com.au/racing/meetings/today/R"
# Log file settings
log_dir = "C:\\PingTestLogs"
log_file_name = "PingTestProjectA1.log"
max_log_size = 2048 # bytes (2 KB)
def ping_url(url):
try:
response = requests.head(url, timeout=5)
if response.status_code == 200:
return True
else:
return False
except requests.RequestException:
return False
def log_ping_result(result):
log_file_path = os.path.join(log_dir, log_file_name)
current_time = datetime.datetime.now().strftime("%Y%m%d:%H:%M:%S")
log_entry = f"-:>{current_time}: {result}\n"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
if os.path.exists(log_file_path):
log_file_size = os.path.getsize(log_file_path)
if log_file_size >= max_log_size:
os.remove(log_file_path)
with open(log_file_path, "a") as log_file:
log_file.write(log_entry)
def main():
for i in range(num_pings):
result = ping_url(url)
if result:
log_ping_result("Ping successful")
else:
log_ping_result("Ping failed")
time.sleep(interval)
if name == "main":
main()
#Here's the Python code for the ping test:
import subprocess
import sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])
import requests
import os
import datetime
import time
import requests
# Adjustable variables
num_pings = 7
interval = 1 # seconds
# URL to ping
url = "
https://tab.com.au/racing/meetings/today/R"
# Log file settings
log_dir = "C:\\PingTestLogs"
log_file_name = "PingTestProjectA1.log"
max_log_size = 2048 # bytes (2 KB)
def ping_url(url):
try:
response = requests.head(url, timeout=5)
if response.status_code == 200:
return True
else:
return False
except requests.RequestException:
return False
def log_ping_result(result):
log_file_path = os.path.join(log_dir, log_file_name)
current_time = datetime.datetime.now().strftime("%Y%m%d:%H:%M:%S")
log_entry = f"-:>{current_time}: {result}\n"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
if os.path.exists(log_file_path):
log_file_size = os.path.getsize(log_file_path)
if log_file_size >= max_log_size:
os.remove(log_file_path)
with open(log_file_path, "a") as log_file:
log_file.write(log_entry)
def main():
for i in range(num_pings):
result = ping_url(url)
if result:
log_ping_result("Ping successful")
else:
log_ping_result("Ping failed")
time.sleep(interval)
if name == "main":
main()
Run Error Message
File "C:\Users\LAPTOP\PyCharmMiscProject\script.py", line 55
result = ping_url(url)
^^^^^^
IndentationError: expected an indented block after 'for' statement on line 54
Process finished with exit code 1
Run Error Message:
File "C:\Users\LAPTOP\PyCharmMiscProject\script.py", line 55
result = ping_url(url)
^^^^^^
IndentationError: expected an indented block after 'for' statement on line 54
Process finished with exit code 1