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.

testwnet.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import os
  2. import win32api
  3. import win32wnet
  4. from winnetwk import *
  5. possible_shares = []
  6. def _doDumpHandle(handle, level=0):
  7. indent = " " * level
  8. while 1:
  9. items = win32wnet.WNetEnumResource(handle, 0)
  10. if len(items) == 0:
  11. break
  12. for item in items:
  13. try:
  14. if item.dwDisplayType == RESOURCEDISPLAYTYPE_SHARE:
  15. print(indent + "Have share with name:", item.lpRemoteName)
  16. possible_shares.append(item)
  17. elif item.dwDisplayType == RESOURCEDISPLAYTYPE_GENERIC:
  18. print(
  19. indent + "Have generic resource with name:", item.lpRemoteName
  20. )
  21. else:
  22. # Try generic!
  23. print(indent + "Enumerating " + item.lpRemoteName, end=" ")
  24. k = win32wnet.WNetOpenEnum(
  25. RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, item
  26. )
  27. print()
  28. _doDumpHandle(k, level + 1)
  29. win32wnet.WNetCloseEnum(
  30. k
  31. ) # could do k.Close(), but this is a good test!
  32. except win32wnet.error as details:
  33. print(indent + "Couldn't enumerate this resource: " + details.strerror)
  34. def TestOpenEnum():
  35. print("Enumerating all resources on the network - this may take some time...")
  36. handle = win32wnet.WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, None)
  37. try:
  38. _doDumpHandle(handle)
  39. finally:
  40. handle.Close()
  41. print("Finished dumping all resources.")
  42. def findUnusedDriveLetter():
  43. existing = [
  44. x[0].lower() for x in win32api.GetLogicalDriveStrings().split("\0") if x
  45. ]
  46. handle = win32wnet.WNetOpenEnum(RESOURCE_REMEMBERED, RESOURCETYPE_DISK, 0, None)
  47. try:
  48. while 1:
  49. items = win32wnet.WNetEnumResource(handle, 0)
  50. if len(items) == 0:
  51. break
  52. xtra = [i.lpLocalName[0].lower() for i in items if i.lpLocalName]
  53. existing.extend(xtra)
  54. finally:
  55. handle.Close()
  56. for maybe in "defghijklmnopqrstuvwxyz":
  57. if maybe not in existing:
  58. return maybe
  59. raise RuntimeError("All drive mappings are taken?")
  60. def TestConnection():
  61. if len(possible_shares) == 0:
  62. print("Couldn't find any potential shares to connect to")
  63. return
  64. localName = findUnusedDriveLetter() + ":"
  65. for share in possible_shares:
  66. print("Attempting connection of", localName, "to", share.lpRemoteName)
  67. try:
  68. win32wnet.WNetAddConnection2(share.dwType, localName, share.lpRemoteName)
  69. except win32wnet.error as details:
  70. print("Couldn't connect: " + details.strerror)
  71. continue
  72. # Have a connection.
  73. try:
  74. fname = os.path.join(localName + "\\", os.listdir(localName + "\\")[0])
  75. try:
  76. print(
  77. "Universal name of '%s' is '%s'"
  78. % (fname, win32wnet.WNetGetUniversalName(fname))
  79. )
  80. except win32wnet.error as details:
  81. print(
  82. "Couldn't get universal name of '%s': %s"
  83. % (fname, details.strerror)
  84. )
  85. print("User name for this connection is", win32wnet.WNetGetUser(localName))
  86. finally:
  87. win32wnet.WNetCancelConnection2(localName, 0, 0)
  88. # and do it again, but this time by using the more modern
  89. # NETRESOURCE way.
  90. nr = win32wnet.NETRESOURCE()
  91. nr.dwType = share.dwType
  92. nr.lpLocalName = localName
  93. nr.lpRemoteName = share.lpRemoteName
  94. win32wnet.WNetAddConnection2(nr)
  95. win32wnet.WNetCancelConnection2(localName, 0, 0)
  96. # and one more time using WNetAddConnection3
  97. win32wnet.WNetAddConnection3(0, nr)
  98. win32wnet.WNetCancelConnection2(localName, 0, 0)
  99. # Only do the first share that succeeds.
  100. break
  101. def TestGetUser():
  102. u = win32wnet.WNetGetUser()
  103. print("Current global user is", repr(u))
  104. if u != win32wnet.WNetGetUser(None):
  105. raise RuntimeError("Default value didnt seem to work!")
  106. TestGetUser()
  107. TestOpenEnum()
  108. TestConnection()