CodyKochmann
8/3/2016 - 3:06 PM

This redirects http couchdb requests going to 5984 and sends them to 6984 for https connections over SSL

This redirects http couchdb requests going to 5984 and sends them to 6984 for https connections over SSL

# -*- coding: utf-8 -*-
# @Author: cody
# @Date:   2016-08-03 10:08:07
# @Last Modified 2016-08-03 Your Name>
# @Last Modified time: 2016-08-03 10:18:10

import SimpleHTTPServer
import SocketServer

port_to_redirect_to=6984
port_to_redirect_from=5984
ip_address=''

class myHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        global port_to_redirect_to
        global ip_address
        self.send_response(301)
        self.send_header('Location','https://{}:{}'.format(ip_address,port_to_redirect_to))
        self.end_headers()

def get_ip():
    # returns your network ip
    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("gmail.com",80))
    out=(s.getsockname()[0])
    s.close()
    return out

if len(ip_address)<1:
    ip_address = get_ip()

pywebserver = SocketServer.TCPServer(("", port_to_redirect_from), myHandler)

print 'forwarding port {} to {} for full https connections'.format(
    port_to_redirect_from,
    port_to_redirect_to
)

pywebserver.serve_forever()