Skip to content

Instantly share code, notes, and snippets.

@osullivj osullivj/ssblock

Created Sep 10, 2015
What would you like to do?
# Copyright (c) Babbington Slade 2015
import sys
import socket
import blockspring
# To run this at the command line...
# c:\python27\python --sheet=BlackScholes.xls --age=31
# The JSON msg we send to SpreadServe's BlockSpringServer
CalcAction = {
"MessageType":"Calc", # Key the correct BlockSpringServer handler
"action":"calc", # Key the correct sseng handler
def param_to_list( request, pname):
# extract a parameter from the request object and convert to Python list
# eg 'B5,B6,B7' -> ['B5','B6','B7']
rv = []
ls = request.params.get( pname)
if ls:
rv = [str( lm) for lm in ls.split(',')]
return rv
def spreadserve( request, response):
host = request.params.get( "host")
CalcAction["sheet"] = str( '%s.xls' % request.params.get( "sheet"))
CalcAction["incells"] = param_to_list( request, "incells")
CalcAction["invals"] = param_to_list( request, "invals")
CalcAction["outcells"] = param_to_list( request, "outcells")
msg = str( CalcAction).replace( "'", '"')
sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
sock.connect( ( host, 8891))
sock.sendall( '%s\n' % msg)
# Nasty blocking call!
resp = sock.recv( 1024)
sock.close( )
except BaseException, e:
rv = 'BADSOCK{%s)' % str(e)
response.addOutput( "result", rv)
response.end( )
ro = eval( resp)
if ro.get('result') == 'failed':
# Handle error msg from sseng or blocksvr
rv = '#SSERR(%s)' % ro.get('error')
rv = ro.get('outvals')
rv = '#BADEVAL(%s)' % resp
response.addOutput( "result", rv)
response.end( )
blockspring.define( spreadserve)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.