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!

Index This | I’m short for "configuration file.” What am I?

May 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with a Special ...

New Articles from Academic Learning Partners, Help Expand Lantern’s Use Case Library, ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Your Guide to SPL2 at .conf24!

So, you’re headed to .conf24? You’re in for a good time. Las Vegas weather is just *chef’s kiss* beautiful in ...