rastest.py 4.6 KB

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