如何使用Qt和Python禁用SSL验证?

huangapple go评论71阅读模式
英文:

How to disable SSL verification using Qt and Python?

问题

我使用PythonQt来请求https://some-site.com,代码如下:

def init_request(self):
    req = QtNetwork.QNetworkRequest(QtCore.QUrl('https://some-site.com'))
    self.nam = QtNetwork.QNetworkAccessManager()
    self.nam.finished.connect(self.handle_response)
    self.nam.get(req)

def handle_response(self, reply):
    if reply.error() != QtNetwork.QNetworkReply.NoError:
        print(reply.errorString())
        return

print(reply.errorString())输出SSL握手失败,因为https://some-site.com的SSL证书存在问题。

我知道可以使用curl不安全地连接到服务器:

curl --insecure https://some-site.com

如何在PythonQt中执行相同的连接操作呢?

英文:

I use Python and Qt to request https://some-site.com like this:

def init_request(self):  
    req = QtNetwork.QNetworkRequest(QtCore.QUrl('https://some-site.com'))    
    self.nam = QtNetwork.QNetworkAccessManager()
    self.nam.finished.connect(self.handle_response)        
    self.nam.get(req)
    
def handle_response(self, reply): 
    if reply.error() != QtNetwork.QNetworkReply.NoError:
        print(reply.errorString())
        return

print(reply.errorString()) outputs SSL handshake failed because the https://some-site.com has problem with SSL certificate.

I know, it is possible to insecure connect to the server using curl:

curl --insecure https://some-site.com

How to do the same connect with Python and Qt?

答案1

得分: 0

你可以像这样使用QNetworkReply.ignoreSslErrors()

from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtNetwork import *

app = QApplication()

net_manager = QNetworkAccessManager()
request = QNetworkRequest(QUrl('https://www.google.com'))
reply = net_manager.get(request)
reply.ignoreSslErrors()

def on_finish():
    print(
	
此处为隐藏的内容
发表评论并刷新,方可查看
)
app.quit() reply.finished.connect(on_finish) app.exec_()

另外,你可以通过ignoreSslErrors(errors)来提高安全性,限制错误类型和证书。参考参考文档

英文:

You can use QNetworkReply.ignoreSslErrors() like this.

from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtNetwork import *

app = QApplication()

net_manager = QNetworkAccessManager()
request = QNetworkRequest(QUrl('https://www.google.com'))
reply = net_manager.get(request)
reply.ignoreSslErrors()
def on_finish():
	print(
	
此处为隐藏的内容
发表评论并刷新,方可查看
) app.quit() reply.finished.connect(on_finish); app.exec_()

Also, you can increase security by limiting error types and certificates with ignoreSslErrors(errors). Refer to the reference.

huangapple
  • 本文由 发表于 2023年3月15日 18:20:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定