gpt4 book ai didi

python - QCoreApplication 没有正常终止

转载 作者:行者123 更新时间:2023-12-01 08:58:52 25 4
gpt4 key购买 nike

我是 PyQt5 新手。我编写了一个简单的 CLI 脚本,它发出 GET 请求,打印结果,然后退出并出现错误:“QWaitCondition:线程仍在等待时被销毁”

我做错了什么?我的代码如下所示:

from PyQt5.QtCore import QCoreApplication, QObject, QUrl
from PyQt5.QtNetwork import QNetworkRequest, QNetworkAccessManager, QNetworkReply
import sys

class MainWindow(QObject):
def __init__(self):
super().__init__()

target = "http://pyqt.sourceforge.net/Docs/PyQt5/index.html"
request = QNetworkRequest(QUrl(target))

self.networkAccessManager = QNetworkAccessManager()
#self.networkAccessManager.finished[QNetworkReply].connect(self.headsUp)
self.networkAccessManager.finished.connect(self.onFinished)
self.networkAccessManager.get(request)

def onFinished(self, response): #response is an object of type QNetworkReply
error = response.error()

if (error == QNetworkReply.NoError):
result = response.readAll()
print(response.header(QNetworkRequest.ContentTypeHeader))
print(response.header(QNetworkRequest.LastModifiedHeader))
print(response.header(QNetworkRequest.ContentLengthHeader))
else:
print("There was an error. "+response.errorString())

self.windUp(response)

def windUp(self, replyObject): #replyObject is an object of type QNetworkReply
print("Done")
replyObject.deleteLater()
QCoreApplication.quit()


if __name__ == '__main__':

qApp = QCoreApplication(sys.argv)
w = MainWindow()
sys.exit(qApp.exec_())

最佳答案

当我运行你的代码时,我没有收到任何错误,只是得到一个空答案。

text/html
None
0
Done

这是正确的,因为该网址中没有任何内容,因为如果您使用浏览器,我们会看到它将我们重定向到另一个网址:https://sourceforge.net/projects/pyqt/,因此为了启用该重定向,将分析回复的 RedirectionTargetAttribute 属性。

import sys
from PyQt5 import QtCore, QtNetwork


class MainWindow(QtCore.QObject):
def __init__(self):
super().__init__()
target = "http://pyqt.sourceforge.net"
self.manager = QtNetwork.QNetworkAccessManager(self)
self.manager.finished.connect(self.on_finished)
self.start_request(target)

def start_request(self, url):
request = QtNetwork.QNetworkRequest(QtCore.QUrl(url))
self.manager.get(request)

@QtCore.pyqtSlot(QtNetwork.QNetworkReply)
def on_finished(self, reply):
redirection_target = reply.attribute(QtNetwork.QNetworkRequest.RedirectionTargetAttribute)
if reply.error():
print("There was an error. {}".format(response.errorString()))
elif redirection_target is not None:
reply.deleteLater()
print("redirection_target: {}".format(redirection_target))
self.start_request(redirection_target)
else:
print(reply.header(QtNetwork.QNetworkRequest.ContentTypeHeader))
print(reply.header(QtNetwork.QNetworkRequest.LastModifiedHeader))
print(reply.header(QtNetwork.QNetworkRequest.ContentLengthHeader))
print(reply.readAll())
reply.deleteLater()
QtCore.QCoreApplication.quit()


if __name__ == '__main__':
app = QtCore.QCoreApplication(sys.argv)
w = MainWindow()
sys.exit(app.exec_())

输出:

redirection_target: PyQt5.QtCore.QUrl('https://sourceforge.net/projects/pyqt/')
text/html; charset=utf-8
None
None
b'<!doctype html>\n<!-- Server: sfs-consume-8 -->\n<html class="no-js" lang="en">\n <head>\n <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/...

关于python - QCoreApplication 没有正常终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52626888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com