A program that every time it is called updates the time and return

Asked

Viewed 112 times

-1

Good night I’m trying to make a program that tests if the internet is working and if not returns the time that will be stored in a file so I can complain to the internet operator. Since whenever I start this program the return is always the same

from conexao import Programa
from datetime import datetime
import time

url = 'https://www.facebook.com/'
hora = Programa.hora()
conexao = Programa.conectar(url, hora)

while True:
    print(conexao)
    conexao

import requests
import time
from datetime import datetime
class Programa():
    def conectar(url, hora):
        while True:
            try:
                requests.get(url, timeout=5)
                time.sleep(5)
                return 1, hora
            except :
                requests.get('http://10.0.0.1/index.asp', timeout=5)
                return 2, hora
            else:
                return 3, hora

    def desconto(valor_minuto, hora):
        desconto = (int(hora) - int(hora) / 60) - valor_minuto
        return desconto

    def hora():
        hora = datetime.now().hour
        minuto = datetime.now().minute
        segundo = datetime.now().second
        hora_caiu = (f'{hora}:{minuto}:{segundo}')
        return hora_caiu

1 answer

1

The return of your function is always the same because you do not update the hora and doesn’t even test the connection again, you’re just printing multiple and multiple times the call result of a single connection test done. Off that block while that you defined within the method conexao, is useless, because when returning a value, the program exited the method and consequently it leaves the block while.

To fix this problem, you can withdraw the while from within its method and can call Programa.hora() and Programa.conectar(url, hora) inside the repeating block while, thus:

url = 'https://www.facebook.com/'

while True:
    hora = Programa.hora()                  # Obtém a hora do computador
    conexao = Programa.conectar(url, hora)  # Realiza um novo teste de conexão
    print(conexao)                          # Imprime resultado do novo teste                        

There are also some places in your code that can also be improved, for example if you want to get an hour format, you can use this code:

hora = time.localtime()                 # Obtém data e hora do seu computador.
hora = time.strftime("%H:%M:%S", hora)  # Formata em "Hora:Minuto:Segundo"

I don’t know if you know this, but if you ever wish to create an object of Programa, your code may give error, because you did not define in the signature of the methods, the parameter self, which is a parameter that receives an instance.

If it was not purposeful, how about declaring above the method the decorator @staticmethod ? I know that for now this is a slightly more complex subject for you, but it’s good to warn.

class Programa():

    @staticmethod
    def conectar(url, hora):
        try:
            requests.get(url, timeout=5)
            time.sleep(5)
            return 1, hora
        except:
            requests.get('http://10.0.0.1/index.asp', timeout=5)
            return 2, hora
        else:
            return 3, hora

    @staticmethod
    def obterHora():
        hora = time.localtime()                 
        hora = time.strftime("%H:%M:%S", hora)
        return hora

I also don’t think it’s a good idea you create a class just for this kind of thing, your code would look much better within functions.

To understand more about Object-Oriented Programming in Python, click here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.