ClickHouse/dbms/tests/external_dictionaries/http_server.py
proller 57c336f267 HTTPDictionarySource support HTTPS (#510)
* https support in dictionaries

* Style
2017-02-28 01:07:57 +04:00

56 lines
1.8 KiB
Python
Executable File

#!/usr/bin/env python
import os
import socket
import sys
import BaseHTTPServer
import SocketServer
import ssl
import argparse
parser = argparse.ArgumentParser(description = 'Simple http/https server')
parser.add_argument('--https', action='store_true', help = 'Use https')
parser.add_argument('--port', type = int, default = 80, help = 'server port')
parser.add_argument('--host', default = "localhost", help = 'server host')
args = parser.parse_args()
if args.https and args.port == 80:
args.port = 443
class myHTTPServer(SocketServer.ForkingMixIn, BaseHTTPServer.HTTPServer):
address_family = socket.AF_INET6
pass
class myHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/":
self.path = "/http_server.py"
try:
f = open(os.curdir + os.sep + self.path)
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(f.read())
f.close()
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
def do_POST(self):
self.do_GET()
return
try:
server = myHTTPServer(('', args.port), myHandler)
if args.https:
os.system('openssl req -subj "/CN={host}/O=My Company Name LTD./C=US" -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout server.key -out server.crt'.format(host=args.host))
server.socket = ssl.wrap_socket(server.socket, keyfile="server.key", certfile='server.crt', server_side=True)
print 'Started http' + ( 's' if args.https else '' ) + ' server on port' , args.port
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()