我想使用rawHeaderListQNetworkReply读取标头如何执行此操作?我尝试进行迭代,但是它说QNetworkRaply对象不是可迭代的,简单的打印说是<PyQt4.QtNetwork.QNetworkReply object at 0x1837958>

这是我的代码

...request = QNetworkRequest(QUrl('http://www.kg'))manager = QNetworkAccessManager()reply = manager.get(request)'''How to get the list of headers?'''


苏丹

最佳答案

很多方法。您可以使用reply.rawHeaderPairs(),例如:

import pprint, sys
from PyQt4 import QtCore, QtNetwork

app = QtCore.QCoreApplication(sys.argv)
request = QtNetwork.QNetworkRequest(QtCore.QUrl("http://stackoverflow.com"))
manager = QtNetwork.QNetworkAccessManager()
reply = manager.get(request)
while not reply.isFinished():
    app.processEvents()

try:
    # for Qt 4.7 and higher:
    pprint.pprint(reply.rawHeaderPairs())
except AttributeError:
    # for Qt < 4.7:
    for header in reply.rawHeaderList():
        pprint.pprint((header, reply.rawHeader(header)))


另请参见QNetworkReply文档,尤其是rawHeaderrawHeaderListheader和其他方法。

编辑说明:我已经更新了示例,以使其与较旧的Qt / PyQt版本(4.7之前)兼容。

关于python - PyQt4:从rawHeaderList提取值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5693238/

10-12 18:13