Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hyperbolix/24b1696dbb03c6032fb7af1a06b75145 to your computer and use it in GitHub Desktop.
Save hyperbolix/24b1696dbb03c6032fb7af1a06b75145 to your computer and use it in GitHub Desktop.
A python3 HTTP server compatible with Apache Drill HTTP Storage Plugin that demonstrates that a field present on page 1 but absent on page 2 results in an exception when queries span into page 2.
#!/usr/bin/python3
from socketserver import ThreadingMixIn
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
from urllib.parse import urlparse
from urllib.parse import parse_qs
import threading
hostName = "localhost"
serverPort = 8081
class ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
pass
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
parsed_url = urlparse('http://localhost' + self.path).query
param_map = parse_qs(parsed_url)
total_results = int(param_map.get('total_results_val')[0])
page_size = int(param_map.get('page_size_val')[0])
page_token = int(param_map.get('next_page')[0][40:]) if 'next_page' in param_map else 0
print("got parameters: " + str(total_results) + " and " + str(page_size) + " and " + str(page_token))
result_json = {}
result_list = []
i = page_token
while i < total_results and i < page_token+page_size:
result_row = {}
result_row['foo'] = i
result_row["foo_id"] = "ca4b3480-4c27-4bcc-8aa5-a046acb0c7c8"
result_row["bar_id"] = "57c2cd6b-f483-4aee-b536-5b2922284d92"
result_row["foo_name"] = "jkljkljkljkljkl"
result_row["bar_name"] = "asdfasdf"
result_row["some_integer"] = 1662758489
result_row["some_double"] = 2.2449264978773344
if 'next_page' not in param_map:
result_row["first_page"] = "first page only value"
result_list.append(result_row)
i = i + 1
if i < total_results:
result_json['has_more'] = True
result_json['next_page'] = 'asdf'*10 + str(i)
else:
result_json['has_more'] = False
result_json['next_page'] = None
result_json['results'] = result_list
print("output: " + json.dumps(result_json))
self.wfile.write(bytes(json.dumps(result_json), 'utf-8'))
if __name__ == "__main__":
webServer = ThreadingSimpleServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print("Server stopped.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment