Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt: how to use QWebPage with an anonimous proxy

This is driving me crazy.

I want to show in a QWebPage an url, but i want to do it passing by an anonimous proxy.

Code

#setting up the proxy

proxy = QNetworkProxy()
proxy.setHostName("189.75.98.199") #just examples
proxy.setPort(1111)
proxy.setType = QNetworkProxy.HttpProxy

#setting the manager

manager = QNetworkAccessManager()
manager.setProxy(proxy) #setting the proxy on the manager

#setting the proxy as application proxy

QNetworkProxy.setApplicationProxy(proxy) #seems to do nothing..

#web page

webpage = QWebPage()
webpage.setNetworkAccessManager(manager) #maybe.. but it doesn't work

webpage.mainFrame().load(QUrl("http://www.foo.bar") )

I call a test page which tells me the headers (on a php server, so i look at $_SERVER), and remote_addr is always my ip, not the proxy ip.

What's wrong?

Can you help me? I'm using PyQt 4.8.3.

like image 575
apelliciari Avatar asked Oct 11 '22 11:10

apelliciari


1 Answers

got it to work

in this way it works, using setApplicationProxy. I still don't understand why setting proxy parameters after instance creation doesn't work (as in the initial example)

def set_proxy(self,  proxy):

    proxy_url = QUrl(proxy)

    if unicode(proxy_url.scheme()).startswith('http'):
        protocol = QNetworkProxy.HttpProxy
    else:
        protocol = QNetworkProxy.Socks5Proxy
    QNetworkProxy.setApplicationProxy(
        QNetworkProxy(
            protocol,
            proxy_url.host(),
            proxy_url.port(),
            proxy_url.userName(),
            proxy_url.password()))
like image 165
apelliciari Avatar answered Nov 15 '22 07:11

apelliciari