Skip to content

Commit 4548718

Browse files
committed
Initial support for proxies. Related to #111 #86
1 parent f5b4243 commit 4548718

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

SPARQLWrapper/Wrapper.py

+15
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ def __init__(self, endpoint, updateEndpoint=None, returnFormat=XML, defaultGraph
290290
self._defaultGraph = defaultGraph
291291
self.onlyConneg = False # Only Content Negotiation
292292
self.customHttpHeaders = {}
293+
self.proxies = {}
293294

294295
if returnFormat in _allowedFormats:
295296
self._defaultReturnFormat = returnFormat
@@ -344,6 +345,15 @@ def setTimeout(self, timeout):
344345
"""
345346
self.timeout = int(timeout)
346347

348+
def setProxies(self, proxies):
349+
"""Set the proxies to use for querying the endpoint.
350+
@since: 1.8.3
351+
352+
@param proxies: Dictionary mapping protocol names to URLs of proxies.
353+
@type proxies: dict
354+
"""
355+
self.proxies = proxies
356+
347357
def setOnlyConneg(self, onlyConneg):
348358
"""Set this option for allowing (or not) only HTTP Content Negotiation (so dismiss the use of HTTP parameters).
349359
@since: 1.8.1
@@ -745,6 +755,11 @@ def _createRequest(self):
745755
for customHttpHeader in self.customHttpHeaders:
746756
request.add_header(customHttpHeader, self.customHttpHeaders[customHttpHeader])
747757

758+
if self.proxies:
759+
proxy_support = urllib2.ProxyHandler(self.proxies)
760+
opener = urllib2.build_opener(proxy_support)
761+
urllib2.install_opener(opener)
762+
748763
return request
749764

750765
def _query(self):

test/proxy_test.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import inspect
5+
import os
6+
import sys
7+
import urllib
8+
9+
# prefer local copy to the one which is installed
10+
# hack from http://stackoverflow.com/a/6098238/280539
11+
_top_level_path = os.path.realpath(os.path.abspath(os.path.join(
12+
os.path.split(inspect.getfile(inspect.currentframe()))[0],
13+
".."
14+
)))
15+
if _top_level_path not in sys.path:
16+
sys.path.insert(0, _top_level_path)
17+
# end of hack
18+
19+
from SPARQLWrapper import SPARQLWrapper, XML, GET, SELECT
20+
import unittest
21+
from urllib2 import URLError
22+
import socket
23+
import errno
24+
25+
endpoint = "http://dbpedia.org/sparql"
26+
endpoint_ssl = "https://dbpedia.org/sparql"
27+
28+
prefixes = """
29+
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
30+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
31+
"""
32+
33+
selectQuery = """
34+
SELECT ?label
35+
WHERE {
36+
<http://dbpedia.org/resource/Asturias> rdfs:label ?label .
37+
}
38+
"""
39+
40+
proxiesWorking = {
41+
"http":"177.22.107.236:8080",
42+
"https":"177.22.107.236:8080"} # Checked using http://www.proxy-checker.org/
43+
44+
proxiesNotWorking = {"http":"127.0.0.1:80",
45+
"https":"127.0.0.1:80"}
46+
47+
class SPARQLWrapperProxyTests(unittest.TestCase):
48+
49+
def __generic(self, endpoint):
50+
sparql = SPARQLWrapper(endpoint)
51+
sparql.setQuery(prefixes + selectQuery)
52+
sparql.setReturnFormat(XML)
53+
sparql.setMethod(GET)
54+
return sparql
55+
56+
def testProxyWorking(self):
57+
sparql = self.__generic(endpoint)
58+
sparql.setProxies(proxiesWorking)
59+
self.assertTrue(sparql.proxies) # assert no empty
60+
61+
try:
62+
result = sparql.query()
63+
except URLError as error:
64+
print "The Proxy server is not responding"
65+
print error # Because we are not sure that the proxy is working
66+
else:
67+
result.convert().toprettyxml()
68+
self.assertTrue(True)
69+
70+
def testProxyWorkingSSL(self):
71+
sparql = self.__generic(endpoint_ssl)
72+
self.assertEqual(sparql.endpoint, endpoint_ssl)
73+
sparql.setProxies(proxiesWorking)
74+
self.assertTrue(sparql.proxies) # assert no empty
75+
76+
try:
77+
result = sparql.query()
78+
except URLError as error:
79+
print "The Proxy server is not responding"
80+
print error # Because we are not sure that the proxy is working
81+
else:
82+
result.convert().toprettyxml()
83+
self.assertTrue(True)
84+
85+
def testProxyNotWorking(self):
86+
sparql = self.__generic(endpoint)
87+
sparql.setProxies(proxiesNotWorking)
88+
self.assertTrue(sparql.proxies) # assert no empty
89+
90+
try:
91+
result = sparql.query()
92+
except URLError as error:
93+
self.assertTrue(True)
94+
else:
95+
self.assertTrue(False)
96+
97+
if __name__ == "__main__":
98+
unittest.main()
99+
100+

test/wrapper_test.py

+9
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,15 @@ def testSetHTTPAuth(self):
312312
self.wrapper.http_auth = "OAuth"
313313
self.assertRaises(NotImplementedError, self._get_request, self.wrapper)
314314

315+
def testSetProxies(self):
316+
proxies = {"http": "127.0.0.1"}
317+
self.assertFalse(self.wrapper.proxies)
318+
self.wrapper.setProxies(proxies)
319+
self.assertTrue(self.wrapper.proxies)
320+
self.assertEqual(self.wrapper.proxies, proxies)
321+
self.assertIsInstance(urllib2._opener, urllib2.OpenerDirector)
322+
323+
315324
def testSetQuery(self):
316325
self.wrapper.setQuery('PREFIX example: <http://example.org/INSERT/> SELECT * WHERE {?s ?p ?v}')
317326
self.assertEqual(SELECT, self.wrapper.queryType)

0 commit comments

Comments
 (0)