Python: passing url through variable not working
下面的代码是用python连接到hp qc alm的,当对值进行硬编码时,它会按预期工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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传递到变量like时
1 | hpQCURL="http://hpqcurl.org" |
然后像这样传递变量:
我收到以下错误:
1 2 | File"<COMObject TDApiOle80.TDConnection.1>", line 2, in InitConnectionEx pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147023174), None) |
ZZU1〔0〕
适用于我,但您也可以在类范围之外全局初始化变量。在本例中,我定义了一个静态变量,这就是为什么我需要用这种方式调用它:qc_ConnectorClass.var但请看一下这个答案,了解初始化位置的重要性(在Python中定义类变量的正确方法)