Getting Data In

Cannot open or write to file from custom controller

DaleFRice
Explorer

I've been experimenting with creating a custom controller for an splunk webapp we're working on. Ultimately, we want to be able to pass values to the controller and have it write those to a file on the server. I am able to get the webapp to call the controller, pass it values, and have it send a return, but if I add commands to open a file on the server it gives a "path not found" error (which I suspect is caused by Splunk not being able to run the script).

Here's the code that does run:

import logging
import os
import sys
import cherrypy

import splunk
import splunk.bundle as bundle
import splunk.appserver.mrsparkle.controllers as controllers
import splunk.appserver.mrsparkle.lib.util as util
from splunk.appserver.mrsparkle.lib.decorators import expose_page

class testService(controllers.BaseController):
    '''A test controller, to practice calling controllers from inside SPLUNK'''
    @expose_page(must_login=True, methods=['GET']) 
    def show(self, **kwargs):
        return self.render_template('/boti:/templates/WriteFile.html')

    @expose_page(must_login=True, methods=['POST'])
    def DocumentWriter(self, **params):
        return self.render_template('/boti:/templates/DocumentWritten.html')

When I change the DocumentWriter method like so:

@expose_page(must_login=True, methods=['POST'])
    def DocumentWriter(self, **params):
        outfile = open("find_me.txt", "w+")
        outfile.write("Found you")
        outfile.close()
        return self.render_template('/boti:/templates/DocumentWritten.html')

it won't even load the show() method. Is it simply not possible to read/write files on the server from a controller, or am I doing something horribly wrong?

Tags (3)
0 Karma
1 Solution

DaleFRice
Explorer

Fixed it. Changed the DocumentWriter code to the following:

@expose_page(must_login=True, trim_spaces = True, methods=['POST'])
def DocumentWriter(self, **params):

    output='<p>'
    for key,val in params.iteritems():
            output+=key+": "+val+"<br>"
    output+='</p>'

    writer=open("*file location*","w+")
    writer.write(output)
    writer.close()
    return output

Not sure why trim_spaces=true was so important, but it works now.

View solution in original post

0 Karma

DaleFRice
Explorer

Fixed it. Changed the DocumentWriter code to the following:

@expose_page(must_login=True, trim_spaces = True, methods=['POST'])
def DocumentWriter(self, **params):

    output='<p>'
    for key,val in params.iteritems():
            output+=key+": "+val+"<br>"
    output+='</p>'

    writer=open("*file location*","w+")
    writer.write(output)
    writer.close()
    return output

Not sure why trim_spaces=true was so important, but it works now.

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...