Python:通过变量传递 url 无效

Python: passing url through variable not working

我在 python 下面的代码中连接到 HP QC ALM,当值被硬编码时它按预期工作:

from win32com.client import Dispatch
class QC_ConnectorClass(object):
def __init__(self):
    print("class init")

def ConnectToQC(self):
    #HP QC OTA methods
    self.TD = Dispatch("TDApiOle80.TDConnection.1")
    self.TD.InitConnectionEx("http://hpqcurl.org")
    self.TD.Login("UName","Pwd")
    self.TD.Connect("Domain","project")
    if self.TD.Connected == True:
        print("Logged in")
        self.TD.Logout();
        print("Logged out")
        self.TD.ReleaseConnection();
    else:
        print("Login failed")

将 hp qc url 传递给变量

hpQCURL="http://hpqcurl.org" 

并像这样传递变量:

self.TD.InitConnectionEx(hpQCURL)

我收到以下错误:

File "<COMObject TDApiOle80.TDConnection.1>", line 2, in InitConnectionEx
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147023174), None)

</p> <pre><code>from win32com.client import Dispatch class QC_ConnectorClass(object): var = "http://hpqcurl.org" def __init__(self): print("class init") def ConnectToQC(self): #HP QC OTA methods self.TD = Dispatch("TDApiOle80.TDConnection.1") self.TD.InitConnectionEx(QC_ConnectorClass.var) self.TD.Login("UName","Pwd") self.TD.Connect("Domain","project") if self.TD.Connected == True: print("Logged in") self.TD.Logout(); print("Logged out") self.TD.ReleaseConnection(); else: print("Login failed")

为我工作,但您也可以在 class 范围之外全局初始化变量。在这种情况下,我定义了一个静态变量,这就是我需要以这种方式调用它的原因:QC_ConnectorClass.var 但是看看这个答案来理解初始化位置的重要性 (correct way to define class variables in Python)