2014-09-03 4 views
2

J'utilise getpage() pour charger les pages:getPage Twisted utilisant proxy

d = getPage(url) 
d.addCallback(parsePage,url) 
d.addErrback(downloadError,url) 

Maintenant, vous devez télécharger via proxy http. Comment puis-je appeler getpage() pour utiliser le proxy http?

Répondre

3

Utilisez plutôt twisted.web.client.ProxyAgent. getPage est l'ancienne API de client HTTP de Twisted, qui n'est pas très bonne. IAgent est la nouvelle et meilleure API client HTTP. En dehors de ses autres avantages, il a également plus de fonctionnalités que getPage - y compris le support des proxies HTTP.

Voici un exemple:

from __future__ import print_function 

from os import environ 

from twisted.internet.task import react 
from twisted.internet.endpoints import HostnameEndpoint 
from twisted.web.client import ProxyAgent 

def main(reactor, proxy_hostname): 
    endpoint = HostnameEndpoint(reactor, proxy_hostname, 80) 
    agent = ProxyAgent(endpoint) 
    return agent.request(b"GET", b"http://google.com/").addCallback(print) 

react(main, [environ["HTTP_PROXY"]]) 
+0

Seriez-vous en mesure de fournir un peu plus de détails à cette réponse? – NT3RP

Questions connexes