CodyKochmann
8/16/2016 - 1:24 PM

This is a really lightweight socket server that serves a python function on a network. Basically, you have a function that provides an IO se

This is a really lightweight socket server that serves a python function on a network. Basically, you have a function that provides an IO service that is wrapped in a python function. What this does is hosts the function on a specified port and allows other devices to use that function to process different things.

I specifically built this to make an IO service for a raspberry pi network so larger ai jobs can be broken into many little parts that each pi can do.

# -*- coding: utf-8 -*-
# @Author: cody
# @Date:   2016-08-16 09:27:53
# @Last Modified 2016-08-16
# @Last Modified time: 2016-08-16 10:46:40

"""
This is basically the interphase to the IO service. Once a machine is
running a hosted function, another machine can use this function to
run data against the function in order to spread a huge job out to
multiple machines.

One example usage of this for myself is a linear regression service.
Since linear regression is a great tool for AI to generate an
understanding of something, I would set up nodes that are ready to
crank out the linear regression of any given dataset per request. This
gave me a tool that would show things like progressing regression
patterns and allowed me to generate on the fly a universe of tests in
order to create more intelligent conclusions.
"""

import socket
from time import sleep

def send_data_to_hosted_function(data, host='', port=80):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host,port))
    s.send(data)
    sleep(1)
    s.send('submit')
    response=''
    while 1:
        data = s.recv(1)
        #print "recieved: {}".format(data)
        if not data:
            break
        response += data
    s.close()
    return response


# test code below
if __name__ == '__main__':
    test_data_to_send="hello world or something like that"
    host='192.168.0.7'
    port=5559
    print send_data_to_hosted_function(test_data_to_send, host, port)

# -*- coding: utf-8 -*-
# @Author: codykochmann
# @Date:   2016-08-16 09:16:48
# @Last Modified 2016-08-16
# @Last Modified time: 2016-08-16 10:38:23

"""
This is a really lightweight socket server that
serves a python function on a network. Basically,
you have a function that provides an IO service
that is wrapped in a python function. What this
does is hosts the function on a specified port
and allows other devices to use that function to
process different things.

I specifically built this to make an IO service
for a raspberry pi network so larger ai jobs can
be broken into many little parts that each pi can
do.
"""

import socket
import thread

def threaded_function_service(conn,input_function):
    """ takes data in a port and responds with a function output """
    collected=''
    while 1:
        data = conn.recv(256)
        print 'parsing: {}'.format(data)
        if data.startswith('submit'):
            break
        if data.endswith("\n") and len(data) is 2:
            data = data[:-2]
            break
        if not data:
            break
        collected+=data
    try:
        reply=input_function(collected)
    except Exception, e:
        reply=e
        pass
    print "responding with: {}".format(reply)
    conn.send(reply)
    conn.close()

def create_io_service(input_function, host='', port=8080):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # set up the socket's settings
    try:
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind((host, port))
        s.listen(5)
    except socket.error as e:
        print(str(e))
    # run an infinite loop that will host this service
    while True:
        conn, addr = s.accept()
        print('connected to: '+addr[0]+':'+str(addr[1]))
        thread.start_new_thread(threaded_function_service,(conn,input_function))

# test code below
if __name__ == "__main__":
    def test_function(input_string):
        """ returns the length of the string given to the function """
        return "recieved string with the length of: {}".format(len(input_string))

    create_io_service(test_function, port=5559)