Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for proxies. #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions SPARQLWrapper/Wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
100 changes: 100 additions & 0 deletions test/proxy_test.py
Original file line number Diff line number Diff line change
@@ -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: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
"""

selectQuery = """
SELECT ?label
WHERE {
<http://dbpedia.org/resource/Asturias> 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()


9 changes: 9 additions & 0 deletions test/wrapper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: <http://example.org/INSERT/> SELECT * WHERE {?s ?p ?v}')
self.assertEqual(SELECT, self.wrapper.queryType)
Expand Down