Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rastest.py 5.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # rastest.py - test/demonstrate the win32ras module.
  2. # Much of the code here contributed by Jethro Wright.
  3. import os
  4. import sys
  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 list(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 as xxx_todo_changeme:
  38. (rc, function, msg) = xxx_todo_changeme.args
  39. print("Can not edit/find the RAS entry -", msg)
  40. def HangUp(hras):
  41. # trap potential, irrelevant errors from win32ras....
  42. try:
  43. win32ras.HangUp(hras)
  44. except:
  45. print("Tried to hang up gracefully on error, but didn't work....")
  46. return None
  47. def Connect(entryName, bUseCallback):
  48. if bUseCallback:
  49. theCallback = Callback
  50. win32event.ResetEvent(callbackEvent)
  51. else:
  52. theCallback = None
  53. # in order to *use* the username/password of a particular dun entry, one must
  54. # explicitly get those params under win95....
  55. try:
  56. dp, b = win32ras.GetEntryDialParams(None, entryName)
  57. except:
  58. print("Couldn't find DUN entry: %s" % entryName)
  59. else:
  60. hras, rc = win32ras.Dial(
  61. None, None, (entryName, "", "", dp[3], dp[4], ""), theCallback
  62. )
  63. # hras, rc = win32ras.Dial(None, None, (entryName, ),theCallback)
  64. # print hras, rc
  65. if not bUseCallback and rc != 0:
  66. print("Could not dial the RAS connection:", win32ras.GetErrorString(rc))
  67. hras = HangUp(hras)
  68. # don't wait here if there's no need to....
  69. elif (
  70. bUseCallback
  71. and win32event.WaitForSingleObject(callbackEvent, 60000)
  72. != win32event.WAIT_OBJECT_0
  73. ):
  74. print("Gave up waiting for the process to complete!")
  75. # sdk docs state one must explcitly hangup, even if there's an error....
  76. try:
  77. cs = win32ras.GetConnectStatus(hras)
  78. except:
  79. # on error, attempt a hang up anyway....
  80. hras = HangUp(hras)
  81. else:
  82. if int(cs[0]) == win32ras.RASCS_Disconnected:
  83. hras = HangUp(hras)
  84. return hras, rc
  85. def Disconnect(rasEntry):
  86. # Need to find the entry
  87. name = rasEntry.lower()
  88. for hcon, entryName, devName, devType in win32ras.EnumConnections():
  89. if entryName.lower() == name:
  90. win32ras.HangUp(hcon)
  91. print("Disconnected from", rasEntry)
  92. break
  93. else:
  94. print("Could not find an open connection to", entryName)
  95. usage = """
  96. Usage: %s [-s] [-l] [-c connection] [-d connection]
  97. -l : List phone-book entries and current connections.
  98. -s : Show status while connecting/disconnecting (uses callbacks)
  99. -c : Connect to the specified phonebook name.
  100. -d : Disconnect from the specified phonebook name.
  101. -e : Edit the specified phonebook entry.
  102. """
  103. def main():
  104. import getopt
  105. try:
  106. opts, args = getopt.getopt(sys.argv[1:], "slc:d:e:")
  107. except getopt.error as why:
  108. print(why)
  109. print(
  110. usage
  111. % (
  112. os.path.basename(
  113. sys.argv[0],
  114. )
  115. )
  116. )
  117. return
  118. bCallback = 0
  119. if args or not opts:
  120. print(
  121. usage
  122. % (
  123. os.path.basename(
  124. sys.argv[0],
  125. )
  126. )
  127. )
  128. return
  129. for opt, val in opts:
  130. if opt == "-s":
  131. bCallback = 1
  132. if opt == "-l":
  133. ShowConnections()
  134. if opt == "-c":
  135. hras, rc = Connect(val, bCallback)
  136. if hras != None:
  137. print("hras: 0x%8lx, rc: 0x%04x" % (hras, rc))
  138. if opt == "-d":
  139. Disconnect(val)
  140. if opt == "-e":
  141. EditEntry(val)
  142. if __name__ == "__main__":
  143. main()