6

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()
obayhan
  • 1,636
  • 18
  • 35
  • the tags must be in the tags section, not in the title – eyllanesc Oct 06 '20 at 22:31
  • I just tested and if the proxy is changed, have you reloaded the page? How do you verify that the second proxy is not used? – eyllanesc Oct 06 '20 at 22:56
  • @eylianesc it is not a tag, it is a library and there is a smilar library called pyside. How do you plan to solve if there is a confusion when people tested my code by using pyside? – obayhan Oct 07 '20 at 07:44
  • Did you tried to change proxy twice? Because it is changing at first time as i wrote but not after it, so i can't use a differrnt proxy rather than first one – obayhan Oct 07 '20 at 07:48
  • Don't use time.sleep(2), check https://stackoverflow.com/questions/41545300/equivalent-to-time-sleep – eyllanesc Oct 07 '20 at 13:24
  • The problem is underlying Qt since PyQt5 or PySide2 are a binding so the tags are irrelevant. – eyllanesc Oct 07 '20 at 13:25
  • @eyllanesc i agree, Because of to preapare sample quickly to explain the situation and for giving people something to try. Normally i dont neither sleep nor print – obayhan Oct 07 '20 at 14:06
  • @eyllanesc thanks for you to help us by stucking that "tag" or whatever situation to improve the question. But it could be better if you helped us with a solution or with a useful idea. PS: I am experienced over 20 years in software and believe me i experienced pyqt and pyside behaves different under some conditions with same code. But i see you know better than me so maybe a simple search result via google can explain it to you: https://stackoverflow.com/questions/6888750/pyqt-or-pyside-which-one-to-use – obayhan Oct 07 '20 at 14:13
  • @obayhan I think it works fine (when I put my single proxy at first pass, I get the connection fine - when I put a dummy proxy for second pass, the web page can not load - at 3rd pass when I put my real proxy again, I get a working connexion again...) (using PyQt5 5.9.2 on python 3.6) – tgrandje Oct 16 '20 at 07:27
  • @tgrandje yes it works if you try it like that but the problem is it doesn't accept second real proxy different than first one. You can check by putting two or more non-transparent proxy from any free proxy site. For example to produce situation try 2 working proxy from here: http://free-proxy.cz/en/ PS: i tried with my paid proxy services(more than one) and also with free proxies – obayhan Oct 16 '20 at 10:33
  • 1
    I will have to try this at home, my office is already working behind one proxy (which is preventing any other web connections...). – tgrandje Oct 16 '20 at 11:22

0 Answers0