Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Using requests with Qt presents problems



  • i have this function

    def ping():
        for key, value in self.board.items():
            word = "hello world!"
            try:
                response = requests.get("http://" + value["ip"], timeout=1)
            except requests.exceptions.Timeout as ex:
                self.board[value["Name"]]['Status'] = "Offline"
            except requests.exceptions.ConnectionError as ex:
                self.board[value["Name"]]['Status'] = "Offline"
    
            if response.status_code == HTTPStatus.OK:
                if word in response.text:
                    self.board[value["Name"]]['Status'] = "Online"
                else:
                    print("got {} instead of {}".format(response.text, word))
            else:
                print("got status {} {}".format(response.status_code, response.reason))
                return False
    

    What's going on here is that i ping 4 Arduino boards and check if the word "Hello World!" is present in the webpage (im gonna change that later btw) so that i know that it is "online".

    When i run my python script pyqt5 waits for this to finish (i counted 4 seconds befor the Gui came up) i've gotten 2 options from people on discord saying

    either

    1. Qthread
    2. use QNetwork / QNetworkRequest / QNetworkAccessManager

    im fairly new to PyQt5 and you could also say Python in general! i've been reading on Qthread AND on QNetworking and well... my brain just shuts down and gives me the "finger" -.-



  • @Kris-Revi

    This is exactly what you need :)
    You just need to translate it into Python code.
    https://stackoverflow.com/questions/46943134/how-do-i-write-a-qt-http-get-request

    To ping all of your devices, you could add their addesses to an array or QStringList and check all IPs

    if(answer.contains("Hello World"))
      {
         qDebug() << "Yeay";
      }
      else
      {
          qDebug() << "Nay";
      }
    


  • keep in mind im new t PyQt5 and compare to "requests" QNetwork and the rest are way more complex :/



  • @Kris-Revi

    The solution from SO works. You just need to add your part, where you check whether the request reply contains your keyword or not. This is why I posted the code sample.
    It's not more complex than the solution, provided on SO.


Log in to reply