英文:
How to disable SSL verification using Qt and Python?
问题
我使用Python
和Qt
来请求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
如何在Python
和Qt
中执行相同的连接操作呢?
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论