- 31 Mar 2023
- 79
- 38
Arada hepimizin interneti gidiyor ve o ara beklerken başka bir şeyler yapıyoruz. İnternet bağlantısının geldiğini kontrol etmek için basit bir program yazdım.
internet_connection.py
utils.py
main.py
Düzgün çalışması için bilgisayarınızda "espeak" in bulunması gerekmektedir.
internet_connection.py
Python:
import requests
from urllib.request import urlopen
from time import sleep, time
from utils import speak
def check_connection(url):
try:
response = requests.get(url, timeout=10)
return True, None
except Exception as e:
return False, e
def check_internet_connection(urls, sleep_time=15, print_errors=False):
error_counter = -1
start_time = time()
while True:
print(f"{time() - start_time:.2f}", end=' ', flush=True)
is_connected, error = check_connection(urls)
if is_connected:
sentence = "İnternet bağlantısı kuruldu!"
speak(sentence)
print("İnternet bağlantısı kuruldu!")
else:
error_counter += 1
if error_counter >= 3:
sentence = "Hâlâ, internet bağlantısı kurulamadı!"
speak(sentence)
print(sentence)
error_counter = 0
if print_errors:
print(f"hata: {error}")
sleep(sleep_time)
utils.py
Python:
import random
from subprocess import call
def speak(sentence):
call(["espeak", "-v", "tr", sentence])
print(sentence)
main.py
Python:
from internet_connection import check_internet_connection
if __name__ == "__main__":
URL = "https://www.google.com"
SLEEP_TIME = 15
print_errors_enabled = False
check_internet_connection(
URL, SLEEP_TIME, print_errors_enabled)
Düzgün çalışması için bilgisayarınızda "espeak" in bulunması gerekmektedir.