Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
try catch not working?
#1
Hello,
I have this code for checking modem connection

import os
import sys
import time
import datetime
import requests
import threading
import socket
import netifaces as ni
import schedule

Error_Count = 0

def get_ip_address():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    return s.getsockname()[0]


def CheckConnection():
    #threading.Timer(60.0, CheckConnection).start() # called every minute
    global Error_Count
    address_check = '8.8.8.8'
    response = os.system('ping -c 2 ' + address_check)
    if response == 0:
        pingstatus = "Network Active"
        print(pingstatus, "count: ", str(Error_Count))
    else:
        print(f'Count is {Error_Count}')
        if Error_Count == 3:
            #os.system('sudo shutdown -r now')
            print('put here reboot command if nothing works , need to check remove route maybe$
            Error_Count = 0
        else:
            errorTime = datetime.datetime.now()
            pingstatus = (str(errorTime) + " : Netwrok Down!")
            OutInterface = get_ip_address()
            print('trying to go out using : ' + OutInterface)
            time.sleep(2)
            try:
                boot_response = os.system('bash /bin/modem_reset >> /home/pi/logs/modem_restar$
            except Exception as e2:
                print(e2)
            print(str(boot_response) + '\n\r Modem shoud be up , see IP')
            time.sleep(5)
            # ni.ifaddresses('wwan0')
            try:
                Wan_ip = ni.ifaddresses('wwan0')[ni.AF_INET][0]['addr']
            except ValueError as ve1:
                print(ve1)
                Wan_ip='x.x.x.x'
            else:
                print('Wan ip - ', Wan_ip)
            try:
                Vpn_ip = ni.ifaddresses('tun0')[ni.AF_INET][0]['addr']
            except ValueError as ve:
                print(ve)
                Vpn_ip='y.y.y.y'
            else:
                print('vpn ip - ', Vpn_ip)
            Error_Count += 1
    #return pingstatus

#threading.Timer(60.0, CheckConnection).start() # called every minute
schedule.every(1).minutes.do(CheckConnection)

while True:
    schedule.run_pending()
    time.sleep(1)
whenever he get to the try-catch for printing wan_ip - I get this error

 File "/home/pi/Documents/CheckConnection_new1.py", line 69, in <module>
    schedule.run_pending()
  File "/usr/local/lib/python3.7/dist-packages/schedule/__init__.py", line 780, in run_pending
    default_scheduler.run_pending()
  File "/usr/local/lib/python3.7/dist-packages/schedule/__init__.py", line 100, in run_pending
    self._run_job(job)
  File "/usr/local/lib/python3.7/dist-packages/schedule/__init__.py", line 172, in _run_job
    ret = job.run()
  File "/usr/local/lib/python3.7/dist-packages/schedule/__init__.py", line 661, in run
    ret = self.job_func()
  File "/home/pi/Documents/CheckConnection_new1.py", line 47, in CheckConnection
    Wan_ip = ni.ifaddresses('wwan0')[ni.AF_INET][0]['addr']
KeyError: 2
and it's finish the code
why ?
what did I do wrong , and why the code won't continue ?
maybe because the
schedule.run_pending()
not in try-catch also ?

Thanks,
Reply
#2
You are catching ValueError exceptions, but the program is raising a KeyError.
Reply
#3
Ok
I will catch it and see what cause it .

meanwhile (until it will catch the error again):
can it be because I don't get addr in the response?

Thanks ,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiprocessing queue catch get timeout Pythocodras 1 2,336 Apr-22-2022, 06:01 PM
Last Post: Pythocodras
  twisted: catch return from sql wardancer84 0 1,542 Sep-08-2021, 12:38 PM
Last Post: wardancer84
  how to catch schema error? maiya 0 1,872 Jul-16-2021, 08:37 AM
Last Post: maiya
  is this a good way to catch exceptions? korenron 14 4,734 Jul-05-2021, 06:20 PM
Last Post: hussaind
  pool mysql error - not catch by try\except? korenron 1 2,155 Jul-05-2021, 11:26 AM
Last Post: ibreeden
  try catch question ,get data from main code korenron 7 3,196 Nov-03-2020, 09:28 AM
Last Post: korenron
  catch input type error mcmxl22 5 3,068 Aug-11-2019, 07:33 AM
Last Post: wavic
  How to catch string between two strings SriMekala 3 2,755 May-24-2019, 03:05 AM
Last Post: heiner55
  Python catch mouse click in pure text, no graphics samtal 7 7,287 Sep-10-2018, 03:02 PM
Last Post: samtal
  Python file parsing, can't catch strings in new line Philia 5 3,970 Apr-29-2018, 01:09 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020