Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using requests with Qt presents problems
Forum Updated to NodeBB v4.3 + New Features

Using requests with Qt presents problems

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 287 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by Kris Revi
    #1

    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" -.-

    Pl45m4P 1 Reply Last reply
    0
    • K Kris Revi

      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" -.-

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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";
        }
      

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      1
      • K Offline
        K Offline
        Kris Revi
        wrote on last edited by
        #3

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

        Pl45m4P 1 Reply Last reply
        0
        • K Kris Revi

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

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @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.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved