Skip to content

Commit 5ab6d2f

Browse files
Merge pull request splunk#224 from splunk/examples/explorer-six-compat
Make the explorer example compatible w/ Python 3
2 parents fa2f7f5 + 0eb516e commit 5ab6d2f

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ dist/
2626
examples/searchcommands_app/package/default/commands.conf
2727
examples/searchcommands_app/package/bin/packages
2828
tests/searchcommands/apps/app_with_logging_configuration/*.log
29-
*.observed
29+
*.observed
30+
venv/

examples/explorer/explorer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
except ImportError:
2828
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
2929
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
30-
import urllib
30+
31+
from splunklib.six.moves import urllib
3132

3233
PORT = 8080
3334

@@ -57,7 +58,7 @@ def main(argv):
5758
args.append(('owner', opts.kwargs['owner']))
5859

5960
# Encode these arguments
60-
args = urllib.urlencode(args)
61+
args = urllib.parse.urlencode(args)
6162

6263
# Launch the browser
6364
webbrowser.open("file://%s" % os.path.join(os.getcwd(), "explorer.html?%s" % args))

examples/explorer/server.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616

1717
from __future__ import absolute_import
1818
from __future__ import print_function
19-
import splunklib.six.moves.SimpleHTTPServer
20-
import splunklib.six.moves.socketserver
21-
import urllib2
2219
import sys
23-
import StringIO
24-
from splunklib import six
20+
import os
21+
22+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))
23+
24+
from splunklib.six import iteritems
25+
from splunklib.six.moves import socketserver, SimpleHTTPServer, StringIO, urllib
2526

2627
PORT = 8080
2728

28-
class RedirectHandler(six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler):
29+
30+
class RedirectHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
2931
def do_GET(self):
3032
redirect_url, headers = self.get_url_and_headers()
3133
if redirect_url is None:
@@ -83,13 +85,13 @@ def make_request(self, url, method, data, headers):
8385

8486
try:
8587
# Make the request
86-
request = urllib2.Request(url, data, headers)
88+
request = urllib.Request(url, data, headers)
8789
request.get_method = lambda: method
88-
response = urllib2.urlopen(request)
90+
response = urllib.urlopen(request)
8991

9092
# We were successful, so send the response code
9193
self.send_response(response.code, message=response.msg)
92-
for key, value in six.iteritems(dict(response.headers)):
94+
for key, value in iteritems(dict(response.headers)):
9395
# Optionally log the headers
9496
#self.log_message("%s: %s" % (key, value))
9597

@@ -105,16 +107,16 @@ def make_request(self, url, method, data, headers):
105107

106108
# Copy the response to the output
107109
self.copyfile(response, self.wfile)
108-
except urllib2.HTTPError as e:
110+
except urllib.HTTPError as e:
109111
# On errors, log the response code and message
110112
self.log_message("Code: %s (%s)", e.code, e.msg)
111113

112-
for key, value in six.iteritems(dict(e.hdrs)):
114+
for key, value in iteritems(dict(e.hdrs)):
113115
# On errors, we always log the headers
114116
self.log_message("%s: %s", key, value)
115117

116118
response_text = e.fp.read()
117-
response_file = StringIO.StringIO(response_text)
119+
response_file = StringIO(response_text)
118120

119121
# On errors, we also log the response text
120122
self.log_message("Response: %s", response_text)
@@ -135,10 +137,10 @@ def make_request(self, url, method, data, headers):
135137
# Finally, send the error itself
136138
self.copyfile(response_file, self.wfile)
137139

138-
class ReuseableSocketTCPServer(six.moves.socketserver.TCPServer):
140+
class ReuseableSocketTCPServer(socketserver.TCPServer):
139141
def __init__(self, *args, **kwargs):
140142
self.allow_reuse_address = True
141-
six.moves.socketserver.TCPServer.__init__(self, *args, **kwargs)
143+
socketserver.TCPServer.__init__(self, *args, **kwargs)
142144

143145
def serve(port = PORT):
144146
Handler = RedirectHandler

0 commit comments

Comments
 (0)