<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Cannot open or write to file from custom controller in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Cannot-open-or-write-to-file-from-custom-controller/m-p/89522#M97302</link>
    <description>&lt;P&gt;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).&lt;/P&gt;

&lt;P&gt;Here's the code that does run:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;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')
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;When I change the DocumentWriter method like so:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;@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')
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;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?&lt;/P&gt;</description>
    <pubDate>Tue, 09 Jul 2013 15:34:08 GMT</pubDate>
    <dc:creator>DaleFRice</dc:creator>
    <dc:date>2013-07-09T15:34:08Z</dc:date>
    <item>
      <title>Cannot open or write to file from custom controller</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Cannot-open-or-write-to-file-from-custom-controller/m-p/89522#M97302</link>
      <description>&lt;P&gt;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).&lt;/P&gt;

&lt;P&gt;Here's the code that does run:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;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')
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;When I change the DocumentWriter method like so:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;@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')
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;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?&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2013 15:34:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Cannot-open-or-write-to-file-from-custom-controller/m-p/89522#M97302</guid>
      <dc:creator>DaleFRice</dc:creator>
      <dc:date>2013-07-09T15:34:08Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot open or write to file from custom controller</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Cannot-open-or-write-to-file-from-custom-controller/m-p/89523#M97303</link>
      <description>&lt;P&gt;Fixed it. Changed the DocumentWriter code to the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;@expose_page(must_login=True, trim_spaces = True, methods=['POST'])
def DocumentWriter(self, **params):

    output='&amp;lt;p&amp;gt;'
    for key,val in params.iteritems():
            output+=key+": "+val+"&amp;lt;br&amp;gt;"
    output+='&amp;lt;/p&amp;gt;'

    writer=open("*file location*","w+")
    writer.write(output)
    writer.close()
    return output
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Not sure why trim_spaces=true was so important, but it works now.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2013 21:05:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Cannot-open-or-write-to-file-from-custom-controller/m-p/89523#M97303</guid>
      <dc:creator>DaleFRice</dc:creator>
      <dc:date>2013-07-09T21:05:13Z</dc:date>
    </item>
  </channel>
</rss>

