Hi guys,

What i need is a way to check the available USB ports in windows using python.

The purpose: I'm trying to run on startUp a script that wait to plug in my Token, and after that the script will log me in automatically into the system that I'm using.

Scenario:
1) Waiting the special port to be used.
2) When detect some device in it - starting the application.
3) Auto log in with the Credentials that are on the token.

I think this should be the easiest way to make this work...
Please give some example of your way implementing step 1.

Thank you in advance!

I found a better solution of my problem:

Here it is:

import win32com.client

strComputer = "."

objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")

colItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_PnPEntity")

for objItem in colItems:
    if(objItem.Name!=None and objItem.Name=='USB Token'):
        print("Name:" + ` objItem.Name`)
        print("Status:" + ` objItem.Status`)
        print("Manufacturer:" + ` objItem.Manufacturer`)
        print("DeviceID:" + ` objItem.DeviceID`)
        print("Status:" + ` objItem.Status`)

This give me exactly what I needed!

commented: thanks for sharing +13

Here is more information about this method:

""" ...
Win32_PnPEntity
Description
The Win32_PnPEntity WMI class represents the properties of a Plug and Play device. Plug and Play entities are shown as entries in the Device Manager located in Control Panel.

import win32com.client
def WMIDateStringToDate(dtmDate):
    strDateTime = ""
    if (dtmDate[4] == 0):
        strDateTime = dtmDate[5] + '/'
    else:
        strDateTime = dtmDate[4] + dtmDate[5] + '/'
    if (dtmDate[6] == 0):
        strDateTime = strDateTime + dtmDate[7] + '/'
    else:
        strDateTime = strDateTime + dtmDate[6] + dtmDate[7] + '/'
        strDateTime = strDateTime + dtmDate[0] + dtmDate[1] + dtmDate[2] + dtmDate[3] + " " + dtmDate[8] + dtmDate[9] + ":" + dtmDate[10] + dtmDate[11] +':' + dtmDate[12] + dtmDate[13]
    return strDateTime

strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_PnPEntity")
for objItem in colItems:
    if objItem.Availability != None:
        print "Availability:" + ` objItem.Availability`
    if objItem.Caption != None:
        print "Caption:" + ` objItem.Caption`
    if objItem.ClassGuid != None:
        print "ClassGuid:" + ` objItem.ClassGuid`
    if objItem.ConfigManagerErrorCode != None:
        print "ConfigManagerErrorCode:" + ` objItem.ConfigManagerErrorCode`
    if objItem.ConfigManagerUserConfig != None:
        print "ConfigManagerUserConfig:" + ` objItem.ConfigManagerUserConfig`
    if objItem.CreationClassName != None:
        print "CreationClassName:" + ` objItem.CreationClassName`
    if objItem.Description != None:
        print "Description:" + ` objItem.Description`
    if objItem.DeviceID != None:
        print "DeviceID:" + ` objItem.DeviceID`
    if objItem.ErrorCleared != None:
        print "ErrorCleared:" + ` objItem.ErrorCleared`
    if objItem.ErrorDescription != None:
        print "ErrorDescription:" + ` objItem.ErrorDescription`
    if objItem.InstallDate != None:
        print "InstallDate:" + WMIDateStringToDate(objItem.InstallDate)
    if objItem.LastErrorCode != None:
        print "LastErrorCode:" + ` objItem.LastErrorCode`
    if objItem.Manufacturer != None:
        print "Manufacturer:" + ` objItem.Manufacturer`
    if objItem.Name != None:
        print "Name:" + ` objItem.Name`
    if objItem.PNPDeviceID != None:
        print "PNPDeviceID:" + ` objItem.PNPDeviceID`
    print "PowerManagementCapabilities:"
    strList = " "
    try :
        for objElem in objItem.PowerManagementCapabilities :
            strList = strList + `objElem` + ","
    except:
        strList = strList + 'null'
    print strList
    if objItem.PowerManagementSupported != None:
        print "PowerManagementSupported:" + ` objItem.PowerManagementSupported`
    if objItem.Service != None:
        print "Service:" + ` objItem.Service`
    if objItem.Status != None:
        print "Status:" + ` objItem.Status`
    if objItem.StatusInfo != None:
        print "StatusInfo:" + ` objItem.StatusInfo`
    if objItem.SystemCreationClassName != None:
        print "SystemCreationClassName:" + ` objItem.SystemCreationClassName`
    if objItem.SystemName != None:
        print "SystemName:" + ` objItem.SystemName`

..."""

Have fun :)

Also here is the link that I found the answer: http://www.activexperts.com/admin/scripts/wmi/python/0346/

For me that code looked ugly, maybe something like this simply (the funcition was not called in my computer, so that is left out for simplicity):

import win32com.client

objSWbemServices = win32com.client.Dispatch("WbemScripting.SWbemLocator").ConnectServer(".","root\cimv2")

for item in objSWbemServices.ExecQuery("SELECT * FROM Win32_PnPEntity"):
    print('-'*60)
    for name in ('Availability', 'Caption', 'ClassGuid', 'ConfigManagerUserConfig',
                 'CreationClassName', 'Description','DeviceID', 'ErrorCleared', 'ErrorDescription',
                 'InstallDate', 'LastErrorCode', 'Manufacturer', 'Name', 'PNPDeviceID', 'PowerManagementCapabilities ',
                 'PowerManagementSupported', 'Service', 'Status', 'StatusInfo', 'SystemCreationClassName', 'SystemName'):
        a = getattr(item, name, None)
        if a is not None:
            print('%s: %s' % (name, a))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.