Python Forum

Full Version: Re Try loop for "net use..." failures
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Greetings to you all!
I’m trying to get some files from the remote hosts.
I’m using a ‘net use’ string for mapping drives and getting files.
Some of the hosts intermittently refused a connection.
I thought I would have a retry loop in the code to solve the problem, I’d like to see the number of retries.
For some reason, the snipped does not want to retry… Wall
Here is a "retry" part of the code:
for retry in range(25):
    try:
        print(f" Try - {retry}")
        call("net use Z: /delete /yes")
        call("net use Z: "+"\\\\"+ips+"\\c$\\S_Files /u:SomeUser Somepass") 
        break
    except:
        print ("Connection failed, retrying...") 
        pass            
Thank you in advance!
Any help is appreciated.
What is the call() function?
My bad!
elsewhere in the script:

from subprocess import call
call (net use...)
Get rid of the try/except. Check the return value of subprocess.call(instruction) to determine if the instruction was successful or not. An instruction that returns an error does not raise an exception.
I see, I'll try it. Thank you!
I'm looking at how to 'retry' without the try/except and I have no idea how to force "retry" on failure...
You could use
from subprocess import check_call
for retry in range(25):
    try:
        print(f" Try - {retry}")
        check_call(["net", "use", "Z:", "/delete", "/yes"])
        check_call(["net", "use", "Z:", rf"\\{ips}\c$\S_Files", "/u:SomeUser", "Somepass"]) 
        break
    except CalledProcessError:
        print ("Connection failed, retrying...") 
Here is a snipped that gives me the value from the subprocess call.
        from subprocess import run

        process = run("net use Z: "+"\\\\"+ips+"\\c$\\S_Files /u:SomeUser Somepass") 	
        retcod = process.returncode
        if retcod != 0 :
            print(f" Failed To Connect!!! - {retcod}")
How I could make it re-run 5 times or so?
Thank you again.
Gribouillis, thank you for the snipped, but it produces errors:

line 373, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['net', 'use', 'Z:', '/delete', '/yes']' returned non-zero exit status 2.

During handling of the above exception, another exception occurred:
line 94, in <module>
except CalledProcessError as ss:
NameError: name 'CalledProcessError' is not defined

Thank you!
(Mar-02-2024, 07:50 AM)tester_V Wrote: [ -> ]NameError: name 'CalledProcessError' is not defined
from subprocess import check_call, CalledProcessError
Pages: 1 2