RUT956 - Failed to establish a new connection RUT9M_R_00.07.17.1

RUT956 - Failed to establish a new connectionI get when I tried to log on using the WebAPI:Error: HTTPConnectionPool(host=‘192.168.1.1’, port=80): Max retries exceeded with url: /api/login (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x000002C5F5DA7E00>: FaError: HTTPCoError: HTTPConnectionPool(host=‘192.168.1.1’, port=80): Max retries exceeded with url: /api/login (Caused Error: HTTPConnectionPool(host=‘192.168.1.1’, port=80): Max retries exceeded with url: /api/login (Caused by NewConnectionError(’<urllib3.connection.HTTPConnection object at 0x000002C5F5DA7E00>: Failed to establish a new connection: [WinError 10061] Kan ikke koble til fordi målmaskinen avslo tilkobling’))

When I use another RUT956 (RUT9M_R_00.07.17.1) there is no problem.

My code is:

type or paste code here

try:
    import secrets
except ModuleNotFoundError:
    print("Lib secrets must be installed")
    print(" $ pip3 install secrets")

try:
    import requests
except ModuleNotFoundError:
    print("Lib requests must be installed")
    print(" $ pip3 install requests")

from AbstractRouter import AbstractRouter

class RouterNew(AbstractRouter):
    def __init__(self) -> None:
        self.username = "admin"
        self.password = "XXXXXXXXXXXXXXXX"        
        self.base_url = "http://192.168.1.1/api"
        self.token = None
        self.token_expires = datetime.now() - timedelta(seconds = 60)

    def login(self):
        credentials = {"username": self.username, "password": self.password}
        try:
            response = requests.post(url=self.base_url + "/login", json=credentials)
            response.raise_for_status()

            if response.status_code == 200:
                data = response.json()
                self.token_expires = datetime.now() + timedelta(seconds = int(data["data"]["expires"]) - 5)
                self.token = data["data"]["token"]
                return data["data"]
            else:
                return f"Unexpected status code: {response.status_code}"
        except requests.exceptions.RequestException as e:
            print(f"Error: {e}")
            return False
        except Exception as e:
            print(f"Error: {e}")
            return False

    def GetStatus(self):
        if self.token_expires < datetime.now():
            self.login()
        headers = {"Authorization": f"Bearer {self.token}"}
        url = f"{self.base_url}/io/status"
        try:
            response = requests.get(url, headers=headers)
            response.raise_for_status()

            if response.status_code == 200:
                response_data = response.json()
                return response_data
            else:
                return f"Unexpected status code: {response.status_code}"
        except requests.exceptions.RequestException as e:
            return f"Error: {e}"

if __name__ == "__main__":
    router = RouterNew()
    print("NewRouter: ", router.NewRouter())
    print(router.GetStatus())


What is it I am doing wrong?

Regards

Terje

Hello,

Thank you for reaching out. A few things to double-check on your side:

  • Are you sure your PC is in the same subnet as the router?
  • Is the RUT956 device’s default LAN IP actually 192.168.1.1?
  • Can the 192.168.1.1 be pinged/reached from your machine?
  • Is HTTP access enabled on the device under System → Administration → Access Control page?

Additionally, one note on your code: in the __main__ function, there’s a router.NewRouter() call, but that method doesn’t exist, which should raise an AttributeError.

Best regards,

1 Like

Hi

The sulution was:

  • Is HTTP access enabled on the device under System → Administration → Access Control page?

It was not on.

NewRouter - The function was not added to the script. Sorry about that. The function is like this:

    def NewRouter(self)-> bool:
        flag: bool = True
        try:
            if self.token_expires < datetime.now():
                flag = self.login()

            if flag == False: 
                return False   
            else:
                headers = {"Authorization": f"Bearer {self.token}"}
                url = f"{self.base_url}/firmware/device/status"
                response = requests.get(url, headers=headers)
                response.raise_for_status()

                if response.status_code == 200:
                    response_data = response.json()
                    a = response_data["data"]["version"].split(".")
                    return (int(a[1]) >= 7 and int(a[2]) >= 14)
                else:
                    print(f"Unexpected status code: {response.status_code}")
                    return False
        except requests.exceptions.RequestException as e:
            print(f"Error: {e}")
            return False

It tells me that verison of the RUT956. If the version is higher or equal to 7.14, I can use the new Web API. And if not, I have to relay on the old version.

1 Like

Hi @TerjeMyk,

Thank you for the update. I assume the issue is now resolved? If so, may we go ahead and close this thread?

If you have any further questions or need assistance with anything else, please feel free to let me know here.

Best regards,

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.