home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32_Demos_rastest.py < prev    next >
Encoding:
Text File  |  2001-07-26  |  4.2 KB  |  137 lines

  1. # rastest.py - test/demonstrate the win32ras module.
  2. # Much of the code here contributed by Jethro Wright.
  3.  
  4. import sys
  5. import string
  6. import os
  7. import win32ras
  8.  
  9. # Build a little dictionary of RAS states to decent strings.
  10. # eg win32ras.RASCS_OpenPort -> "OpenPort"
  11. stateMap = {}
  12. for name, val in win32ras.__dict__.items():
  13.     if name[:6]=="RASCS_":
  14.         stateMap[val] = name[6:]
  15.  
  16. # Use a lock so the callback can tell the main thread when it is finished.
  17. import win32event
  18. callbackEvent = win32event.CreateEvent(None, 0, 0, None)
  19.  
  20. def Callback( hras, msg, state, error, exterror):
  21. #    print "Callback called with ", hras, msg, state, error, exterror
  22.     stateName = stateMap.get(state, "Unknown state?")
  23.     print "Status is %s (%04lx), error code is %d" % (stateName, state, error)
  24.     finished = state in [win32ras.RASCS_Connected]
  25.     if finished:
  26.         win32event.SetEvent(callbackEvent)
  27.     if error != 0 or int( state ) == win32ras.RASCS_Disconnected:
  28.         #    we know for sure this is a good place to hangup....
  29.         print "Detected call failure: %s" % win32ras.GetErrorString( error )
  30.         HangUp( hras )
  31.         win32event.SetEvent(callbackEvent)
  32.  
  33. def ShowConnections():
  34.     print "All phone-book entries:"
  35.     for (name,) in win32ras.EnumEntries():
  36.         print " ", name
  37.     print "Current Connections:"
  38.     for con in win32ras.EnumConnections():
  39.         print " ", con
  40.  
  41. def EditEntry(entryName):
  42.     try:
  43.         win32ras.EditPhonebookEntry(0,None,entryName)
  44.     except win32ras.error, (rc, function, msg):
  45.         print "Can not edit/find the RAS entry -", msg
  46.  
  47. def HangUp( hras ):
  48.     #    trap potential, irrelevant errors from win32ras....
  49.     try:
  50.         win32ras.HangUp( hras )
  51.     except:
  52.         print "Tried to hang up gracefully on error, but didn't work...."
  53.     return None
  54.  
  55. def Connect(entryName, bUseCallback):
  56.     if bUseCallback:
  57.         theCallback = Callback
  58.         win32event.ResetEvent(callbackEvent)
  59.     else:
  60.         theCallback = None
  61.     #    in order to *use* the username/password of a particular dun entry, one must 
  62.     #    explicitly get those params under win95....
  63.     try:
  64.         dp, b = win32ras.GetEntryDialParams( None, entryName )
  65.     except:
  66.         print "Couldn't find DUN entry: %s" % entryName
  67.     else:
  68.         hras, rc = win32ras.Dial(None, None, (entryName, "", "", dp[ 3 ], dp[ 4 ], ""),theCallback)
  69.     #    hras, rc = win32ras.Dial(None, None, (entryName, ),theCallback)
  70.     #    print hras, rc
  71.         if not bUseCallback and rc <> 0:
  72.             print "Could not dial the RAS connection:", win32ras.GetErrorString(rc)
  73.             hras = HangUp( hras )
  74.         #    don't wait here if there's no need to....
  75.         elif bUseCallback and win32event.WaitForSingleObject(callbackEvent, 60000)!=win32event.WAIT_OBJECT_0:
  76.             print "Gave up waiting for the process to complete!"
  77.             #    sdk docs state one must explcitly hangup, even if there's an error....
  78.             try:
  79.                 cs = win32ras.GetConnectStatus( hras )
  80.             except:
  81.                 #    on error, attempt a hang up anyway....
  82.                 hras = HangUp( hras )
  83.             else:
  84.                 if int( cs[ 0 ] ) == win32ras.RASCS_Disconnected:
  85.                     hras = HangUp( hras )
  86.     return hras, rc
  87.  
  88. def Disconnect( rasEntry ):
  89.     # Need to find the entry
  90.     name = string.lower( rasEntry )
  91.     for hcon, entryName, devName, devType in win32ras.EnumConnections():
  92.         if string.lower( entryName ) == name:
  93.             win32ras.HangUp( hcon )
  94.             print "Disconnected from", rasEntry
  95.             break
  96.     else:
  97.         print "Could not find an open connection to", entryName
  98.  
  99. usage = """
  100. Usage: %s [-s] [-l] [-c connection] [-d connection]
  101. -l : List phone-book entries and current connections.
  102. -s : Show status while connecting/disconnecting (uses callbacks)
  103. -c : Connect to the specified phonebook name.
  104. -d : Disconnect from the specified phonebook name.
  105. -e : Edit the specified phonebook entry.
  106. """
  107.  
  108. def main():
  109.     import getopt
  110.     try:
  111.         opts, args = getopt.getopt(sys.argv[1:], "slc:d:e:")
  112.     except getopt.error, why:
  113.             print why
  114.             print usage % (os.path.basename(sys.argv[0],))
  115.             return
  116.  
  117.     bCallback = 0
  118.     if args or not opts:
  119.         print usage % (os.path.basename(sys.argv[0],))
  120.         return
  121.     for opt, val in opts:
  122.         if opt=="-s":
  123.             bCallback = 1
  124.         if opt=="-l":
  125.             ShowConnections()
  126.         if opt=="-c":
  127.             hras, rc = Connect(val, bCallback)
  128.             if hras != None:
  129.                 print "hras: 0x%8lx, rc: 0x%04x" % ( hras, rc )
  130.         if opt=="-d":
  131.             Disconnect(val)
  132.         if opt=="-e":
  133.             EditEntry(val)
  134.  
  135. if __name__=='__main__':
  136.     main()
  137.