Python:通过变量传递URL不起作用

时间:2018-11-27 12:12:14

标签: python python-3.x qc

我已经在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)

1 个答案:

答案 0 :(得分:3)

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")

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