2020-04-25 21:21:36 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import subprocess
|
|
|
|
import threading
|
|
|
|
from io import StringIO, SEEK_END
|
|
|
|
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
|
|
|
|
CLICKHOUSE_HOST = os.environ.get('CLICKHOUSE_HOST', '127.0.0.1')
|
|
|
|
CLICKHOUSE_PORT_HTTP = os.environ.get('CLICKHOUSE_PORT_HTTP', '8123')
|
|
|
|
|
|
|
|
# IP-address of this host accessible from outside world.
|
2020-04-26 12:04:28 +00:00
|
|
|
HTTP_SERVER_HOST = os.environ.get('HTTP_SERVER_HOST', subprocess.check_output(['hostname', '-i']).decode('utf-8').strip())
|
2020-04-25 21:21:36 +00:00
|
|
|
HTTP_SERVER_PORT = int(os.environ.get('CLICKHOUSE_TEST_HOST_EXPOSED_PORT', 51234))
|
|
|
|
|
|
|
|
# IP address and port of the HTTP server started from this script.
|
|
|
|
HTTP_SERVER_ADDRESS = (HTTP_SERVER_HOST, HTTP_SERVER_PORT)
|
|
|
|
HTTP_SERVER_URL_STR = 'http://' + ':'.join(str(s) for s in HTTP_SERVER_ADDRESS) + "/"
|
|
|
|
|
|
|
|
ostream = StringIO()
|
2020-04-30 15:40:29 +00:00
|
|
|
istream = sys.stdout
|
2020-04-25 21:21:36 +00:00
|
|
|
|
|
|
|
class EchoCSVHTTPServer(BaseHTTPRequestHandler):
|
|
|
|
def _set_headers(self):
|
|
|
|
self.send_response(200)
|
2020-04-26 14:23:55 +00:00
|
|
|
self.send_header('Content-type', 'text/plain')
|
2020-04-25 21:21:36 +00:00
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
self._set_headers()
|
|
|
|
with open(CSV_DATA, 'r') as fl:
|
|
|
|
ostream.seek(0)
|
|
|
|
for row in ostream:
|
|
|
|
self.wfile.write(row + '\n')
|
|
|
|
return
|
|
|
|
|
|
|
|
def read_chunk(self):
|
|
|
|
msg = ''
|
|
|
|
while True:
|
|
|
|
sym = self.rfile.read(1)
|
|
|
|
if sym == '':
|
|
|
|
break
|
|
|
|
msg += sym.decode('utf-8')
|
|
|
|
if msg.endswith('\r\n'):
|
|
|
|
break
|
|
|
|
length = int(msg[:-2], 16)
|
|
|
|
if length == 0:
|
|
|
|
return ''
|
|
|
|
content = self.rfile.read(length)
|
|
|
|
self.rfile.read(2) # read sep \r\n
|
|
|
|
return content.decode('utf-8')
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
while True:
|
|
|
|
chunk = self.read_chunk()
|
|
|
|
if not chunk:
|
|
|
|
break
|
2020-04-26 11:48:43 +00:00
|
|
|
istream.write(chunk)
|
2020-04-30 15:40:29 +00:00
|
|
|
istream.flush()
|
2020-04-25 21:21:36 +00:00
|
|
|
text = ""
|
|
|
|
self._set_headers()
|
|
|
|
self.wfile.write("ok")
|
|
|
|
|
|
|
|
def log_message(self, format, *args):
|
|
|
|
return
|
|
|
|
|
|
|
|
def start_server(requests_amount, test_data="Hello,2,-2,7.7\nWorld,2,-5,8.8"):
|
|
|
|
ostream = StringIO(test_data.decode("utf-8"))
|
|
|
|
|
|
|
|
httpd = HTTPServer(HTTP_SERVER_ADDRESS, EchoCSVHTTPServer)
|
|
|
|
|
|
|
|
def real_func():
|
|
|
|
for i in xrange(requests_amount):
|
|
|
|
httpd.handle_request()
|
|
|
|
|
|
|
|
t = threading.Thread(target=real_func)
|
|
|
|
return t
|
|
|
|
|
2020-04-30 15:40:29 +00:00
|
|
|
def run(requests_amount=1):
|
|
|
|
t = start_server(requests_amount)
|
2020-04-25 21:21:36 +00:00
|
|
|
t.start()
|
|
|
|
t.join()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
exception_text = ''
|
|
|
|
for i in range(1, 5):
|
|
|
|
try:
|
2020-04-30 15:40:29 +00:00
|
|
|
run(int(sys.argv[1]) if len(sys.argv) > 1 else 1)
|
2020-04-25 21:21:36 +00:00
|
|
|
break
|
|
|
|
except Exception as ex:
|
|
|
|
exception_text = str(ex)
|
2020-04-30 15:40:29 +00:00
|
|
|
time.sleep(1)
|
2020-04-25 21:21:36 +00:00
|
|
|
|
|
|
|
if exception_text:
|
|
|
|
print("Exception: {}".format(exception_text), file=sys.stderr)
|
|
|
|
os._exit(1)
|
|
|
|
|