2012-05-22 96 views
1

我正在編寫一個ssl客戶端到我的服務器,它使用Python與pyqt4扭曲,我用QTReactor在PYQT中扭曲,但是當我運行代碼時出現錯誤AttributeError:'NoneType'對象沒有屬性'connectSSL'

AttributeError: 'NoneType' object has no attribute 'connectSSL' 

我最初是這樣的代碼

from OpenSSL import SSL 
import sys 
from twisted.internet.protocol import ClientFactory 
from twisted.protocols.basic import LineReceiver 
from twisted.internet import ssl 
import qt4reactor 

app = QtGui.QApplication(sys.argv) 
reactor=qt4reactor.install() 
main() 
myapp = MainForm() 
myapp.show() 
reactor.runReturn() 
sys.exit(app.exec_()) 

def main(): 
    factory = ClientFactory() 
    reactor.connectSSL('localhost', 8080, factory, ssl.ClientContextFactory()) 
    try: 
     reactor.run() 
    except KeyboardInterrupt: 
     reactor.stop() 

的錯誤,當我運行它:

Traceback (most recent call last): 
    File "client.py", line 51, in <module> 
    main() 
    File "client.py", line 40, in main 
    reactor.connectSSL('localhost', 8080, factory, ssl.ClientContextFactory()) 
AttributeError: 'NoneType' object has no attribute 'connectSSL' 

回答

6

AttributeError: 'NoneType' object has no attribute 'connectSSL'

是在嘗試調用None上的方法時得到的錯誤消息。

這行代碼

reactor=qt4reactor.install() 

是其中reactor被分配的唯一地方。該錯誤消息表明reactor正在分配值None

所有的網絡搜索點擊,我可以找到關於該主題遵循此模式:

qt4reactor.install(app) 
from twisted.internet import reactor 

,所以我想這是你如何打算這樣做。但我確實承認完全不瞭解這些框架。

2

qt4reactor.install()不會返回值,因此reactor結果爲None。所以當你試圖調用reactor的方法時,你會得到這個錯誤(顯然,None沒有任何方法)。如果我看到它正確,獲取reactor變量的正確方法是:

qt4reactor.install(app) 
from twisted.internet import reactor