Python Forum
Re Try loop for "net use..." failures
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Re Try loop for "net use..." failures
#1
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.
Reply
#2
What is the call() function?
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
My bad!
elsewhere in the script:

from subprocess import call
call (net use...)
Reply
#4
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.
tester_V likes this post
Reply
#5
I see, I'll try it. Thank you!
Reply
#6
I'm looking at how to 'retry' without the try/except and I have no idea how to force "retry" on failure...
Reply
#7
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...") 
tester_V likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#8
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.
Reply
#9
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!
Reply
#10
(Mar-02-2024, 07:50 AM)tester_V Wrote: NameError: name 'CalledProcessError' is not defined
from subprocess import check_call, CalledProcessError
tester_V likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply


Forum Jump:

User Panel Messages

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