[python-win32] win32ras.Dial

Tim Golden tim.golden at viacom-outdoor.co.uk
Mon Jan 9 10:45:41 CET 2006


[Lai, Chern]

| We have successfully initiated a connection via the win32ras.Dial
| command but it bombed out on the win32ras.HangUp command every time.
| Here is our code:
| 
| import win32ras
|  
| # Dialup with a RAS entry
| hdl, retcode = win32ras.Dial (
|     None, 
|     None, 
|     ("windows_ras_entry_name", "", "", "username", "password", ""), 
|     None
|   )
|  
| # Hangup the connection
| win32ras.HangUp (hdl)

I haven't the means to test this at the moment,
but the following code has worked for me
in the past. I've got a feeling that the win32ras.Dial
call returns asynchronously, so if your code is
doing exactly what it says, the connection may still
be being made when you try to hang up. (If that
is the case, why are you trying to do it anyway?)

On the surface, I can't see anything wrong, but
you might want to try the EnumConnections approach
in case hanging up on a partial or non-existent
connection is a no-no.

If there's still no joy, perhaps you could post
the interpreter session and include the traceback?

TJG

<code>
"""redial.py - Attempt to redial a given connection at a
 determined frequency
"""
import os, sys
import time
import traceback
import tempfile
import win32ras

#
# Output log info to %TEMP%/redial.log
#
LOGFILE = os.path.join (tempfile.gettempdir (), "redial.log")

def log (text):
  open (LOGFILE, "a").write ("%s %s\n" % (time.asctime (), text))

def connect ():
  return win32ras.Dial (
    None, 
    None, 
    ("VONET", "", "", "username", "password", ""), 
    None
  )

def disconnect_all ():
  for handle, name, devtype, devname in win32ras.EnumConnections ():
    print "Hanging up", name
    win32ras.HangUp (handle)

#
# Wait for 2 minutes between each check
#
SNOOZE_SECS = 120 

#
# Retry up to three times,
#  waiting RETRY_GAP_SECS seconds 
#  between each try
#
N_RETRIES = 3
RETRY_GAP_SECS = 10

def main ():

  while 1:
    try:
      handle, result = connect ()

      if result == 0:
        log ("Connected with handle %d" % handle)
      else:
        log ("Problem - %s" % win32ras.GetErrorString (result))
        disconnect_all ()

        for n_retry in range (N_RETRIES):
          handle, result = connect ()
          if result == 0:
            log ("Reconnected with handle %d")
            break
          else:
            time.sleep (RETRY_GAP_SECS)

      time.sleep (SNOOZE_SECS)

    except KeyboardInterrupt:
      disconnect_all ()
      sys.exit ()
    except:
      traceback.print_exc (file=LOGFILE)
      disconnect_all ()

if __name__ == '__main__':
  try:
    main ()
  finally:
    disconnect_all ()
</code>

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________


More information about the Python-win32 mailing list