Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4943

Python • Re: Autostart python program terminal window & restart after crash

$
0
0
It's the Python code that crashes.... I am not fluent in Python but understand the basics... The part that is crashing is the socket connections and other Internet aspects that I am not fluent in ..
Ah, a learning opportunity ;-)

I find many times with those sort of calls you need to wrap them in a while/try&except to catch known errors and retry a few times before aborting. Here's an example with FTP. Different protocol, same idea.

Code:

        while True:            try:                with FTP(ftp_host, cfg["ftp_user"], ftp_pass,timeout=420) as ftp: # V5.4.6 / V6.1                    with open(local_file, 'rb') as image_file:                        ftp_response=ftp.storbinary('STOR '+upload_name, image_file)                        err_no=ftp_response[0:3]                        if err_no != "226":                            msg="FTP_UPLOAD: ftplib error "+ftp_response+" on file "+local_file                            success = False                        else:                            success=True                            msg="Local file "+local_file+" uploaded to "+ upload_name                break                             except:                exc_type, exc_value, exc_traceback = sys.exc_info()                type=exc_type.__name__                summ = str(traceback.extract_tb(exc_traceback))                err_value=str(exc_value) # V2.4 #V6.6                if (err_value.find("Temporary failure in name resolution") != -1) or \                   (err_value.find("Gateway Time-out") != -1): # V6.3 V6.5 V6.6                    retry_cnt +=1                    if retry_cnt > 4:                        msg = "FTP_UPLOAD: ERROR - NAME RESOLUTION/GATEWAY TIMEOUT retry exceeded"                        print(pdt()+msg)                        break                    else:                        print(pdt()+"FTP_UPLOAD: Warning - NAME RESOLUTION/GATEWAY TIMEOUT error. Retrying in 60 seconds. Retry count "+\                              str(retry_cnt))                        time.sleep(120) #V6.7                        continue                                    msg="FTP_UPLOAD Exception'"+type+"="+str(exc_value)+"'. Traceback: "+summ                success=False                break         # End of While True             

Statistics: Posted by DS256 — Sat Jun 29, 2024 1:28 pm



Viewing all articles
Browse latest Browse all 4943

Trending Articles