diff --git a/SPARQLWrapper/Wrapper.py b/SPARQLWrapper/Wrapper.py index a288fe0..6a54b59 100644 --- a/SPARQLWrapper/Wrapper.py +++ b/SPARQLWrapper/Wrapper.py @@ -290,6 +290,7 @@ def __init__(self, endpoint, updateEndpoint=None, returnFormat=XML, defaultGraph self._defaultGraph = defaultGraph self.onlyConneg = False # Only Content Negotiation self.customHttpHeaders = {} + self.proxies = {} if returnFormat in _allowedFormats: self._defaultReturnFormat = returnFormat @@ -344,6 +345,15 @@ def setTimeout(self, timeout): """ self.timeout = int(timeout) + def setProxies(self, proxies): + """Set the proxies to use for querying the endpoint. + @since: 1.8.3 + + @param proxies: Dictionary mapping protocol names to URLs of proxies. + @type proxies: dict + """ + self.proxies = proxies + def setOnlyConneg(self, onlyConneg): """Set this option for allowing (or not) only HTTP Content Negotiation (so dismiss the use of HTTP parameters). @since: 1.8.1 @@ -745,6 +755,11 @@ def _createRequest(self): for customHttpHeader in self.customHttpHeaders: request.add_header(customHttpHeader, self.customHttpHeaders[customHttpHeader]) + if self.proxies: + proxy_support = urllib2.ProxyHandler(self.proxies) + opener = urllib2.build_opener(proxy_support) + urllib2.install_opener(opener) + return request def _query(self): diff --git a/test/proxy_test.py b/test/proxy_test.py new file mode 100644 index 0000000..6d8a0ff --- /dev/null +++ b/test/proxy_test.py @@ -0,0 +1,100 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import inspect +import os +import sys +import urllib + +# prefer local copy to the one which is installed +# hack from http://stackoverflow.com/a/6098238/280539 +_top_level_path = os.path.realpath(os.path.abspath(os.path.join( + os.path.split(inspect.getfile(inspect.currentframe()))[0], + ".." +))) +if _top_level_path not in sys.path: + sys.path.insert(0, _top_level_path) +# end of hack + +from SPARQLWrapper import SPARQLWrapper, XML, GET, SELECT +import unittest +from urllib2 import URLError +import socket +import errno + +endpoint = "http://dbpedia.org/sparql" +endpoint_ssl = "https://dbpedia.org/sparql" + +prefixes = """ + PREFIX rdf: + PREFIX rdfs: +""" + +selectQuery = """ + SELECT ?label + WHERE { + rdfs:label ?label . + } +""" + +proxiesWorking = { + "http":"177.22.107.236:8080", + "https":"177.22.107.236:8080"} # Checked using http://www.proxy-checker.org/ + +proxiesNotWorking = {"http":"127.0.0.1:80", + "https":"127.0.0.1:80"} + +class SPARQLWrapperProxyTests(unittest.TestCase): + + def __generic(self, endpoint): + sparql = SPARQLWrapper(endpoint) + sparql.setQuery(prefixes + selectQuery) + sparql.setReturnFormat(XML) + sparql.setMethod(GET) + return sparql + + def testProxyWorking(self): + sparql = self.__generic(endpoint) + sparql.setProxies(proxiesWorking) + self.assertTrue(sparql.proxies) # assert no empty + + try: + result = sparql.query() + except URLError as error: + print "The Proxy server is not responding" + print error # Because we are not sure that the proxy is working + else: + result.convert().toprettyxml() + self.assertTrue(True) + + def testProxyWorkingSSL(self): + sparql = self.__generic(endpoint_ssl) + self.assertEqual(sparql.endpoint, endpoint_ssl) + sparql.setProxies(proxiesWorking) + self.assertTrue(sparql.proxies) # assert no empty + + try: + result = sparql.query() + except URLError as error: + print "The Proxy server is not responding" + print error # Because we are not sure that the proxy is working + else: + result.convert().toprettyxml() + self.assertTrue(True) + + def testProxyNotWorking(self): + sparql = self.__generic(endpoint) + sparql.setProxies(proxiesNotWorking) + self.assertTrue(sparql.proxies) # assert no empty + + try: + result = sparql.query() + except URLError as error: + self.assertTrue(True) + else: + self.assertTrue(False) + +if __name__ == "__main__": + unittest.main() + + diff --git a/test/wrapper_test.py b/test/wrapper_test.py index 61c5953..4833b9b 100644 --- a/test/wrapper_test.py +++ b/test/wrapper_test.py @@ -312,6 +312,15 @@ def testSetHTTPAuth(self): self.wrapper.http_auth = "OAuth" self.assertRaises(NotImplementedError, self._get_request, self.wrapper) + def testSetProxies(self): + proxies = {"http": "127.0.0.1"} + self.assertFalse(self.wrapper.proxies) + self.wrapper.setProxies(proxies) + self.assertTrue(self.wrapper.proxies) + self.assertEqual(self.wrapper.proxies, proxies) + self.assertIsInstance(urllib2._opener, urllib2.OpenerDirector) + + def testSetQuery(self): self.wrapper.setQuery('PREFIX example: SELECT * WHERE {?s ?p ?v}') self.assertEqual(SELECT, self.wrapper.queryType)