Pregunta

I'm trying to connect to a socks5 proxy using PyQt4. I can do this using a regular proxy but not a socks5 one (which requires a username and password) using the below code I get an error...

TypeError: QNetworkProxy.setPassword(QString): first argument of unbound method must have type 'QNetworkProxy'

The code:

from PyQt4 import QtWebKit
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtNetwork import *


class Render(QtWebKit.QWebPage):
    def __init__(self, url):
        QNetworkProxy.setPassword('password')
        QNetworkProxy.setUser('user')
        QNetworkProxy.setApplicationProxy(QNetworkProxy(QNetworkProxy.Socks5Proxy, "proxy5.com", 1080))

        self.app = QtGui.QApplication(sys.argv)
        QtWebKit.QWebPage.__init__(self)
        self.loadFinished.connect(self._loadFinished)
        self.mainFrame().load(QtCore.QUrl(url))
        self.app.exec_()

    def _loadFinished(self, result):
        self.frame = self.mainFrame()
        self.app.quit()

Would anyone know what I'm doing wrong..??

¿Fue útil?

Solución

The QNetworkProxy.setPassword() is not a class method but an instance method. So create an instance of QNetworkProxy then set user/password on that:

def __init__(self, url):
    networkProxy = QNetworkProxy(QNetworkProxy.Socks5Proxy, "proxy5.com", 1080)
    networkProxy.setPassword('password')
    networkProxy.setUser('user')
    QNetworkProxy.setApplicationProxy(networkProxy)

(I've checked the above is correct syntax, but obviously the socks5 host (proxy5.com), port, user and password would have to be correct). If still doesn't work for you, then problem is somewhere else, please clarify by editing your question.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top