简体   繁体   English

Pyside:QNetworkAccessManager不发送请求

[英]Pyside: QNetworkAccessManager sends no Request

I tried to establish a network connection with PySide (Ubuntu 15.04, Python3.4, PySide 1.2.4). 我试图与PySide(Ubuntu 15.04,Python3.4,PySide 1.2.4)建立网络连接。 I used the example code from the documentation . 我使用了文档中的示例代码。

The QNetworkAccessManager does not send the request and I recieve no answer. QNetworkAccessManager不发送请求,我没有收到任何答复。 I checked the Network state with QNetworkSession(QNetworkConfigurationManager().defaultConfiguration()).State() but it says the State is invalid. 我用QNetworkSession(QNetworkConfigurationManager().defaultConfiguration()).State()检查了网络状态,但它说状态无效。 This seems to make no sense since I am on a desktop pc with a network connection via ethernet cable. 这似乎毫无意义,因为我在台式机上通过以太网电缆进行网络连接。

My complete example for the test is the following code: 我完整的测试示例是以下代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PySide.QtGui import QApplication
from PySide.QtCore import QUrl
from PySide.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkSession, QNetworkConfigurationManager


def replyFinished(reply):
    print(reply)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    manager = QNetworkAccessManager()
    manager.finished.connect(replyFinished)
    print(QNetworkSession(QNetworkConfigurationManager().defaultConfiguration()).State())
    print("Sending request")
    print(manager.get(QNetworkRequest(QUrl("http://www.heise.de/ct/"))))

This prints 此打印

PySide.QtNetwork.QNetworkSession.State.Invalid
Sending request
<PySide.QtNetwork.QNetworkReply object at 0x7f4b59c9af08>

but it should display the PySide.QtNetwork.QNetworkReply object twice. 但它应该两次显示PySide.QtNetwork.QNetworkReply对象。

My example was too small. 我的例子太小了。 I realized this because of the comment of Pavel Strakhov. 由于Pavel Strakhov的评论,我意识到了这一点。 I extended it to display a window with a button. 我将其扩展为显示带有按钮的窗口。 By clicking the button it connects successfully. 通过单击按钮,它成功连接。 QNetworkSession(QNetworkConfigurationManager().defaultConfiguration()).State() is still invalid but it works. QNetworkSession(QNetworkConfigurationManager().defaultConfiguration()).State()仍然无效,但可以工作。

This is the working code: 这是工作代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PySide.QtGui import QApplication, QWidget, QBoxLayout, QPushButton
from PySide.QtCore import QUrl
from PySide.QtNetwork import QNetworkAccessManager, QNetworkRequest,  QNetworkSession, QNetworkConfigurationManager


class Window(QWidget):
   def __init__(self):
        super().__init__()
        self.manager = QNetworkAccessManager()
        self.manager.finished.connect(self.reply_finished)
        layout = QBoxLayout(QBoxLayout.TopToBottom)
        button = QPushButton("connect")
        button.clicked.connect(self.network_connect)
        layout.addWidget(button)
        self.setLayout(layout)
        self.setWindowTitle("Network test")
        self.setGeometry(100, 100, 200, 150)
        self.show()

    def network_connect(self):
        print(QNetworkSession(QNetworkConfigurationManager().defaultConfiguration()).State())
        request = QNetworkRequest(QUrl("http://example.org"))
        print("Sending request")
        self.manager.get(request)

    def reply_finished(self, reply):
        print(reply)
        print(reply.readData(500))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    app.exec_()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM