testwnet.py 3.4 KB

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