PyQt5 - Can't change proxy more than once after QWebEngineView initialized

552 views Asked by At

I followed this solution and worked fine at the beginning.

Connect QWebEngine to proxy

But i cant change proxy again after i change it once or QWebEngineView doesnt cares it.

The original code contains more than that, so i purified it to a working example to demonstrate my problem

Assume our ip is "x.x.x.x", proxy1's ip is "y.y.y.y", and proxy2's ip is "z.z.z.z"

When you run the sample code you have to see

  1. x.x.x.x
  2. y.y.y.y
  3. z.z.z.z

but i got

  1. x.x.x.x
  2. y.y.y.y
  3. y.y.y.y

So, any ideas how to solve that?

Sample running code: (just change proxy infos in test function. You may try any working proxies from here: http://free-proxy.cz/en/).

""" NODOC """
__author__ = 'ozgur'
__creation_date__ = '7.10.2020 14:04'

import sys
import time

from PyQt5 import QtNetwork
from PyQt5.QtCore import QMetaObject, QTimer, QUrl
from PyQt5.QtTest import QTest
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout


class RosaRafined(QMainWindow):

    # noinspection PyUnusedLocal
    def __init__(self, qapp: QApplication):
        super(self.__class__, self).__init__()
        self.qapp = qapp
        self._setup_ui()
        self.counter = 0

    def _setup_ui(self):
        self.setObjectName("Form")
        self.resize(1024, 768)
        self.verticalLayout = QVBoxLayout(self)
        self.verticalLayout.setObjectName("verticalLayout")
        self.webengine = QWebEngineView(self)
        self.webengine.setObjectName("webengine")
        self.webengine.resize(1024, 768)
        # self.webengine.page().profile().setPersistentStoragePath(self.temppath)
        # self.webengine.page().profile().setCachePath(self.temppath)
        self.verticalLayout.addWidget(self.webengine)

        QMetaObject.connectSlotsByName(self)
        QTimer().singleShot(1000, self.test)

    @staticmethod
    def _change_proxy(ip, port, user, passwd):
        proxy = QtNetwork.QNetworkProxy()
        proxy.setType(QtNetwork.QNetworkProxy.HttpProxy)
        proxy.setHostName(ip)
        proxy.setPort(port)
        proxy.setUser(user)
        proxy.setPassword(passwd)
        QtNetwork.QNetworkProxy.setApplicationProxy(proxy)

    def test(self):
        
        testurl = QUrl("https://whatismyipaddress.com")
        if self.counter == 0:
            print("Will show your real ip, visiting url")
            self.webengine.setUrl(testurl)
            # your ip x.x.x.x shown, normally

        elif self.counter == 1:
            self._change_proxy("y.y.y.y", 80, "user", "pass")
            QTest.qWait(2)
            print("Will show your proxy1 ip")
            self.webengine.setUrl(testurl)
            # your ip y.y.y.y shown, normally

        elif self.counter == 2:
            self._change_proxy("x.x.x.x", 80, "user", "pass")
            QTest.qWait(2)
            print("Will show your proxy2 ip, unfortunately not...")
            self.webengine.setUrl(testurl)

            # still seeing y.y.y.y , it is not normal
        else:
            self.qapp.quit()

        self.counter += 1
        QTimer().singleShot(5000, self.test)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    form = RosaRafined(app)
    form.show()
    app.exec_()
    sys.exit()
0

There are 0 answers