Issue
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
?
Solution
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([reply.error(), reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)])
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.
Answered By - relent95
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.