Skip to content

Make the explorer example compatible w/ Python 3 #224

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

Merged
merged 1 commit into from
Oct 10, 2018
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ dist/
examples/searchcommands_app/package/default/commands.conf
examples/searchcommands_app/package/bin/packages
tests/searchcommands/apps/app_with_logging_configuration/*.log
*.observed
*.observed
venv/
5 changes: 3 additions & 2 deletions examples/explorer/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
except ImportError:
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
import urllib

from splunklib.six.moves import urllib

PORT = 8080

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

# Encode these arguments
args = urllib.urlencode(args)
args = urllib.parse.urlencode(args)

# Launch the browser
webbrowser.open("file://%s" % os.path.join(os.getcwd(), "explorer.html?%s" % args))
Expand Down
30 changes: 16 additions & 14 deletions examples/explorer/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@

from __future__ import absolute_import
from __future__ import print_function
import splunklib.six.moves.SimpleHTTPServer
import splunklib.six.moves.socketserver
import urllib2
import sys
import StringIO
from splunklib import six
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))

from splunklib.six import iteritems
from splunklib.six.moves import socketserver, SimpleHTTPServer, StringIO, urllib

PORT = 8080

class RedirectHandler(six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler):

class RedirectHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
redirect_url, headers = self.get_url_and_headers()
if redirect_url is None:
Expand Down Expand Up @@ -83,13 +85,13 @@ def make_request(self, url, method, data, headers):

try:
# Make the request
request = urllib2.Request(url, data, headers)
request = urllib.Request(url, data, headers)
request.get_method = lambda: method
response = urllib2.urlopen(request)
response = urllib.urlopen(request)

# We were successful, so send the response code
self.send_response(response.code, message=response.msg)
for key, value in six.iteritems(dict(response.headers)):
for key, value in iteritems(dict(response.headers)):
# Optionally log the headers
#self.log_message("%s: %s" % (key, value))

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

# Copy the response to the output
self.copyfile(response, self.wfile)
except urllib2.HTTPError as e:
except urllib.HTTPError as e:
# On errors, log the response code and message
self.log_message("Code: %s (%s)", e.code, e.msg)

for key, value in six.iteritems(dict(e.hdrs)):
for key, value in iteritems(dict(e.hdrs)):
# On errors, we always log the headers
self.log_message("%s: %s", key, value)

response_text = e.fp.read()
response_file = StringIO.StringIO(response_text)
response_file = StringIO(response_text)

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

class ReuseableSocketTCPServer(six.moves.socketserver.TCPServer):
class ReuseableSocketTCPServer(socketserver.TCPServer):
def __init__(self, *args, **kwargs):
self.allow_reuse_address = True
six.moves.socketserver.TCPServer.__init__(self, *args, **kwargs)
socketserver.TCPServer.__init__(self, *args, **kwargs)

def serve(port = PORT):
Handler = RedirectHandler
Expand Down