Skip to content

Commit 2041824

Browse files
author
Shakeel Mohamed
committed
Update examples to support Python 2.7.9+
1 parent fb18ca0 commit 2041824

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

examples/async/async.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import sys, os, datetime
2424
import urllib
25+
import ssl
2526
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))
2627

2728
import splunklib.binding as binding
@@ -171,7 +172,12 @@ def request(url, message, **kwargs):
171172
method = message.get("method", "GET")
172173

173174
# Note that we do not support proxies in this example
174-
opener = urllib2.build_opener()
175+
# If running Python 2.7.9+, disable SSL certificate validation
176+
if sys.version_info >= (2, 7, 9):
177+
unverified_ssl_handler = urllib2.HTTPSHandler(context=ssl._create_unverified_context())
178+
opener = urllib2.build_opener(unverified_ssl_handler)
179+
else:
180+
opener = urllib2.build_opener()
175181

176182
# Unfortunately, we need to use the hack of
177183
# "overriding" `request.get_method` to specify
@@ -185,6 +191,7 @@ def request(url, message, **kwargs):
185191
response = opener.open(request)
186192
except Exception as e:
187193
response = e
194+
print e
188195

189196
# Normalize the response to something the SDK expects, and
190197
# return it.

examples/handlers/handler_proxy.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from pprint import pprint
3030
from StringIO import StringIO
3131
import sys, os
32+
import ssl
3233
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))
3334
import urllib2
3435

@@ -52,9 +53,15 @@ def request(url, message, **kwargs):
5253
method = message['method'].lower()
5354
data = message.get('body', "") if method == 'post' else None
5455
headers = dict(message.get('headers', []))
55-
context = urllib2.Request(url, data, headers)
56+
req = urllib2.Request(url, data, headers)
5657
try:
57-
response = urllib2.urlopen(context)
58+
response = urllib2.urlopen(req)
59+
except urllib2.URLError, response:
60+
# If running Python 2.7.9+, disable SSL certificate validation and try again
61+
if sys.version_info >= (2, 7, 9):
62+
response = urllib2.urlopen(req, context=ssl._create_unverified_context())
63+
else:
64+
raise
5865
except urllib2.HTTPError, response:
5966
pass # Propagate HTTP errors via the returned response message
6067
return {

examples/handlers/handler_urllib2.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import sys, os
2222
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))
2323
import urllib2
24+
import ssl
2425

2526
import splunklib.client as client
2627

@@ -34,9 +35,13 @@ def request(url, message, **kwargs):
3435
method = message['method'].lower()
3536
data = message.get('body', "") if method == 'post' else None
3637
headers = dict(message.get('headers', []))
37-
context = urllib2.Request(url, data, headers)
38+
# If running Python 2.7.9+, disable SSL certificate validation
39+
req = urllib2.Request(url, data, headers)
3840
try:
39-
response = urllib2.urlopen(context)
41+
if sys.version_info >= (2, 7, 9):
42+
response = urllib2.urlopen(req, context=ssl._create_unverified_context())
43+
else:
44+
response = urllib2.urlopen(req)
4045
except urllib2.HTTPError, response:
4146
pass # Propagate HTTP errors via the returned response message
4247
return {

0 commit comments

Comments
 (0)