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.

rasutil.py 2.7KB

1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # A demo of using the RAS API from Python
  2. import sys
  3. import win32ras
  4. # The error raised if we can not
  5. class ConnectionError(Exception):
  6. pass
  7. def Connect(rasEntryName, numRetries=5):
  8. """Make a connection to the specified RAS entry.
  9. Returns a tuple of (bool, handle) on success.
  10. - bool is 1 if a new connection was established, or 0 is a connection already existed.
  11. - handle is a RAS HANDLE that can be passed to Disconnect() to end the connection.
  12. Raises a ConnectionError if the connection could not be established.
  13. """
  14. assert numRetries > 0
  15. for info in win32ras.EnumConnections():
  16. if info[1].lower() == rasEntryName.lower():
  17. print("Already connected to", rasEntryName)
  18. return 0, info[0]
  19. dial_params, have_pw = win32ras.GetEntryDialParams(None, rasEntryName)
  20. if not have_pw:
  21. print("Error: The password is not saved for this connection")
  22. print(
  23. "Please connect manually selecting the 'save password' option and try again"
  24. )
  25. sys.exit(1)
  26. print("Connecting to", rasEntryName, "...")
  27. retryCount = numRetries
  28. while retryCount > 0:
  29. rasHandle, errCode = win32ras.Dial(None, None, dial_params, None)
  30. if win32ras.IsHandleValid(rasHandle):
  31. bValid = 1
  32. break
  33. print("Retrying...")
  34. win32api.Sleep(5000)
  35. retryCount = retryCount - 1
  36. if errCode:
  37. raise ConnectionError(errCode, win32ras.GetErrorString(errCode))
  38. return 1, rasHandle
  39. def Disconnect(handle):
  40. if type(handle) == type(""): # have they passed a connection name?
  41. for info in win32ras.EnumConnections():
  42. if info[1].lower() == handle.lower():
  43. handle = info[0]
  44. break
  45. else:
  46. raise ConnectionError(0, "Not connected to entry '%s'" % handle)
  47. win32ras.HangUp(handle)
  48. usage = """rasutil.py - Utilities for using RAS
  49. Usage:
  50. rasutil [-r retryCount] [-c rasname] [-d rasname]
  51. -r retryCount - Number of times to retry the RAS connection
  52. -c rasname - Connect to the phonebook entry specified by rasname
  53. -d rasname - Disconnect from the phonebook entry specified by rasname
  54. """
  55. def Usage(why):
  56. print(why)
  57. print(usage)
  58. sys.exit(1)
  59. if __name__ == "__main__":
  60. import getopt
  61. try:
  62. opts, args = getopt.getopt(sys.argv[1:], "r:c:d:")
  63. except getopt.error as why:
  64. Usage(why)
  65. retries = 5
  66. if len(args) != 0:
  67. Usage("Invalid argument")
  68. for opt, val in opts:
  69. if opt == "-c":
  70. Connect(val, retries)
  71. if opt == "-d":
  72. Disconnect(val)
  73. if opt == "-r":
  74. retries = int(val)