def _fill_suggested_proxies(self): try: import libproxy except ImportError: logger.warning( "Please install libproxy python bindings to enable proxy support." ) return factory = libproxy.ProxyFactory() # Get DirectConnection proxies proxies = factory.getProxies('none://messenger.msn.com:1863') proxies = [self._parse_proxy(p) for p in proxies] self._direct_proxies = proxies # Get HTTP proxies (if not already set by a parameter) if not self._default_http_proxy: proxies = factory.getProxies('http://gateway.messenger.msn.com/') proxies = [self._parse_proxy(p) for p in proxies] self._http_proxies = proxies if len(proxies) > 0: self._default_http_proxy = proxies[0] # Get HTTPS proxies (if not already set by a parameter) if not self._default_https_proxy: proxies = factory.getProxies( 'https://rsi.hotmail.com/rsi/rsi.asmx') proxies = [self._parse_proxy(p) for p in proxies] if len(proxies) > 0: self._default_https_proxy = proxies[0]
def get_proxy(self, url): if not libproxy: return "" pf = libproxy.ProxyFactory() for proxy in pf.getProxies(url): if proxy.startswith('http'): return proxy return None
def __create_opener(self): proxy_factory = libproxy.ProxyFactory() proxies = proxy_factory.getProxies(self._url) if (len(proxies) > 0) and (proxies[0] != 'direct://'): proxy_dict = {'http': proxies[0]} else: proxy_dict = {} handler = urllib2.ProxyHandler(proxy_dict) return urllib2.build_opener(handler)
def get_http_proxy_string_from_libproxy(url): """Helper that uses libproxy to get the http proxy for the given url """ import libproxy pf = libproxy.ProxyFactory() proxies = pf.getProxies(url) # FIXME: how to deal with multiple proxies? proxy = proxies[0] if proxy == "direct://": return "" else: return proxy
def __init__(self, url, params=None, post=False, username=None, password=None, header=None, body=None): self.curl = pycurl.Curl() if header: self.curl.setopt(pycurl.HTTPHEADER, header) if body: self.curl.setopt(pycurl.POST, 1) self.curl.setopt(pycurl.POSTFIELDS, body) if params: if post: self.curl.setopt(pycurl.HTTPPOST, [(x, str(y)) for x,y in params.items()]) else: url = "?".join((url, urllib.urlencode(params))) self.curl.setopt(pycurl.URL, str(url)) #log.logger.debug("URL: %s", str(url)) if username and password: self.curl.setopt(pycurl.USERPWD, "%s:%s" % (str(username), str(password))) self.curl.setopt(pycurl.FOLLOWLOCATION, 1) self.curl.setopt(pycurl.MAXREDIRS, 5) self.curl.setopt(pycurl.TIMEOUT, 150) self.curl.setopt(pycurl.HTTP_VERSION, pycurl.CURL_HTTP_VERSION_1_0) self.content = StringIO.StringIO() self.curl.setopt(pycurl.WRITEFUNCTION, self.content.write) if libproxy: proxy_factory = libproxy.ProxyFactory() log.logger.debug("libproxy: getting proxies") proxylist = proxy_factory.getProxies(str(url)) if proxylist: proxy = proxylist[0] if (proxy.find("@") != -1): self.curl.setopt(pycurl.PROXYAUTH, ["CURLAUTH_ANY"]) if (proxy.find("direct://") != 0): log.logger.debug("using proxy %s", proxy) self.curl.setopt(pycurl.PROXY, proxy) try: self.curl.perform() except pycurl.error, e: log.logger.error("Network failure - error: %d - %s", e[0], e[1])
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see <http://www.gnu.org/licenses/>. import StringIO import urllib import logging import gobject import pycurl try: import libproxy proxy_factory = libproxy.ProxyFactory() except ImportError: libproxy = None from virtaal.common.gobjectwrapper import GObjectWrapper __all__ = ['HTTPClient', 'HTTPRequest', 'RESTRequest'] class HTTPRequest(GObjectWrapper): """Single HTTP request, blocking if used standalone.""" __gtype_name__ = 'HttpClientRequest' __gsignals__ = { "http-success": (gobject.SIGNAL_RUN_LAST, None, (object,)), "http-redirect": (gobject.SIGNAL_RUN_LAST, None, (object,)),