Add proxy's in Python Web Browser Application using PYQT5 labiraries

 How to Add proxy's in Python Web Browser Application using PYQT5 labiraries?




A simple small example with short codes are here for you. I want to add a proxy in PyQt5 web browser I created my own proxy to intercept packets on 127.0.0.1:6666, and I want it so all packets sent from QWebEngine gets sent to the proxy. I looked this up and I found a few websites saying to use QNetworkProxy, but it never showed how to get it to work properly. Here is a code sample I found online for just a normal QWebEngine program but it works fine and look nice. So keep in mind proxy.setUser/setPassword command must be used for authentication proxy to connect server where you can buy some proxies. SetPort option is use to connect port like 7468 and setHostName option filled with IP name of you proxy. Like 127.0.0.12. I hope you understand.
Don't worries if you need proxies to connect server for checking your app. So webshare.io and many others proxy sites are available to buy proxies and give minimum 10 proxies for free.

Thanks for all Reader's

Coding Start here ...

from PyQt5 import QtCore, QtGui, QtWidgets, QtNetwork
class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(800, 600)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
    self.gridLayout.setObjectName("gridLayout")
    self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
    self.webView.setUrl(QtCore.QUrl("http://www.google.com/"))
    self.webView.setObjectName("webView")
    self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
    MainWindow.setCentralWidget(self.centralwidget)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)
    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)
   def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

from PyQt5 import QtWebEngineWidgets

if __name__ == "__main__":
   import sys
   app =QtWidgets.QApplication(sys.argv)

   proxy = QtNetwork.QNetworkProxy()

   proxy.setType(QtNetwork.QNetworkProxy.HttpProxy)

   proxy.setHostName("127.0.0.1")

   proxy.setPort(6666)
   proxy.setUser("userName")
   proxy setPassword("password")

   QtNetwork.QNetworkProxy.setApplicationProxy(proxy)

   MainWindow = QtWidgets.QMainWindow()

   ui = Ui_MainWindow()

   ui.setupUi(MainWindow)

   MainWindow.show()

   sys.exit(app.exec_())
   
   

Post a Comment

0 Comments