Created
July 29, 2017 14:11
-
-
Save rahuliyer95/5ab612ee45d3575a5e136b394b763bad to your computer and use it in GitHub Desktop.
Running redis-server using knc and a proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| knc proxy for redis | |
| Created by iyerrah on 29-July-2017 | |
| """ | |
| from __future__ import print_function | |
| import os | |
| import multiprocessing | |
| import socket | |
| BUFFER_SIZE = 32 * 1024 | |
| def main(): | |
| """ | |
| Driver of the program | |
| """ | |
| if os.path.exists('/tmp/redis.knc.sock'): | |
| os.unlink('/tmp/redis.knc.sock') | |
| knc = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
| knc.bind('/tmp/redis.knc.sock') | |
| knc.listen(1) | |
| redis = socket.socket(socket.AF_UNIX) | |
| redis.connect('/tmp/redis.sock') | |
| while True: | |
| conn, addr = knc.accept() | |
| print('Accepted connection from', addr) | |
| handler = multiprocessing.Process(target=handle_client, args=(conn, redis)) | |
| # handler.daemon = True | |
| handler.start() | |
| def handle_client(client, redis): | |
| """ | |
| Handle the connection from a client and redirect it to redis | |
| :param client: the client connection object | |
| :param redis: socket connection to redis | |
| """ | |
| while True: # Read till connection is alive | |
| data = read_till_no_data(client) | |
| if not data: | |
| break | |
| print('Received from client', data) | |
| # Strip knc's END\n | |
| try: | |
| idx = data.index('END\n') | |
| data = data[idx:] | |
| except ValueError: | |
| pass | |
| redis.sendall(data) | |
| data = read_till_no_data(redis) | |
| if not data: | |
| break | |
| print('Received from redis', data) | |
| client.sendall(data) | |
| client.close() | |
| def read_till_no_data(conn, timeout=0.01): | |
| """ | |
| Read from the socket connection till there is a timeout | |
| :param conn: socket connection to be read from | |
| :param timeout: time in seconds to wait before a timeout. Default is `0.01` | |
| :return: the data read from the socket | |
| """ | |
| conn.settimeout(timeout) | |
| data = None | |
| while True: # Read till no more data | |
| try: | |
| new_data = conn.recv(BUFFER_SIZE) | |
| if data: | |
| data += new_data | |
| else: | |
| data = new_data | |
| except socket.timeout, e: | |
| if not data: | |
| continue | |
| break | |
| except socket.error, e: | |
| print(e) | |
| break | |
| return data | |
| if __name__ == '__main__': | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| redis-server \ | |
| --unixsocket /tmp/redis.sock \ | |
| --unixsocketperm 700 \ | |
| > redis_server.log 2>&1 & | |
| # Wait for the server to start | |
| sleep 1 | |
| # Start the proxy | |
| python redis_knc_proxy.py > redis_knc_proxy.log 2>&1 & | |
| # Wait for the proxy to start up | |
| sleep 1 | |
| exec redis-cli -s /tmp/redis.knc.sock |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment