问题
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.
回答1:
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()))
来源:https://stackoverflow.com/questions/5423013/pyqt-how-to-use-qwebpage-with-an-anonimous-proxy