如何在PyQt4中的http请求中设置X-Forwarded-For

时间:2012-12-19 21:10:51

标签: python qt http-headers ip pyqt

我想知道如何将X-Forwarded-For参数添加到我的标题中。

例如,如果我想在我的应用程序中更改user-uagent,我会做这样的事情:

class MyBrowser(QWebPage):
    ''' Settings for the browser.'''

    def __init__(self):
        QWebPage.__init__(self)

    def userAgentForUrl(self, url):
        ''' Returns a User Agent that will be seen by the website. '''
        return "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15"

而不是我会做的事情:

class Browser(QWebView):
    def __init__(self, user_info):
        QWebView.__init__(self)
        self.setPage(MyBrowser())

1 个答案:

答案 0 :(得分:2)

QWebView有void QWebView::load ( const QNetworkRequest & request, QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation, const QByteArray & body = QByteArray() )。您可以使用void QNetworkRequest::setRawHeader ( const QByteArray & headerName, const QByteArray & headerValue )

在QNetworkRequest上设置任何标头

我不是python开发人员,但在C ++中它会是这样的:

QWebView * view = new QWebView;
QNetworkRequest req(QUrl("http://www.google.pl"));
req.setRawHeader("X-Forwarded-For", "MyData");
req.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15");
view->load(req); // will perform get operation by default
view->show();