Skip to main content
 首页 » 编程设计

python之属性错误 : 'NoneType' object has no attribute 'connectSSL'

2025年05月04日13shangdawei

我正在为我的服务器编写一个 ssl 客户端,它使用 python twisted 和 pyqt4,我在 PYQT 中使用 QTReactor 进行 twisted 但是当我运行代码时出现错误

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' 

请您参考如下方法:

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

是您尝试在 None 上调用方法时收到的错误消息。

这行代码

reactor=qt4reactor.install() 

是唯一分配 reactor 的地方。错误消息清楚地表明正在为 reactor 分配值 None。

我能找到的关于该主题的所有网络搜索结果都遵循以下模式:

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

所以我想这就是您的本意。但我承认我对这些框架一无所知。