<?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 Help with python wrapper! in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Help-with-python-wrapper/m-p/144905#M2007</link>
    <description>&lt;P&gt;I'm trying to execute a python script as a scripted input. My script needs external modules to run so i've taken a wrapper script I found on the answers site and used it to call my external python and script. However, splunk does not pickup the output because the wrapper script immediately goes back to the command prompt before the other script finishes. See wrapper code below and example output. Please help!&lt;/P&gt;

&lt;P&gt;OUTPUT&lt;BR /&gt;
D:\Splunk\bin&amp;gt;splunk cmd python d:\Splunk\bin\scripts\globalapps\google_analytic&lt;BR /&gt;
s_wrapper.py&lt;/P&gt;

&lt;P&gt;D:\Splunk\bin&amp;gt;02/10/14 17:11:37 activeVisitors=889&lt;/P&gt;

&lt;H2&gt;Wrapper code&lt;/H2&gt;

&lt;P&gt;import os, sys&lt;BR /&gt;
import time&lt;/P&gt;

&lt;H3&gt;DELETING ENV VARS&lt;/H3&gt;

&lt;H1&gt;Remove problematic environmental variables if they exist.&lt;/H1&gt;

&lt;P&gt;for envvar in ("PYTHONPATH", "LD_LIBRARY_PATH"):&lt;BR /&gt;
    if envvar in os.environ:&lt;BR /&gt;
        del os.environ[envvar]&lt;/P&gt;

&lt;H3&gt;USING OS INTERPRETER&lt;/H3&gt;

&lt;P&gt;python_executable = "C:\Python27\python.exe"&lt;/P&gt;

&lt;P&gt;real_script = os.path.join(os.path.dirname(&lt;STRONG&gt;file&lt;/STRONG&gt;), "real_time_analytics.py")&lt;/P&gt;

&lt;H2&gt;os.execv(python_executable, [ python_executable, real_script ] )&lt;/H2&gt;</description>
    <pubDate>Mon, 28 Sep 2020 15:51:21 GMT</pubDate>
    <dc:creator>jedatt01</dc:creator>
    <dc:date>2020-09-28T15:51:21Z</dc:date>
    <item>
      <title>Help with python wrapper!</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Help-with-python-wrapper/m-p/144905#M2007</link>
      <description>&lt;P&gt;I'm trying to execute a python script as a scripted input. My script needs external modules to run so i've taken a wrapper script I found on the answers site and used it to call my external python and script. However, splunk does not pickup the output because the wrapper script immediately goes back to the command prompt before the other script finishes. See wrapper code below and example output. Please help!&lt;/P&gt;

&lt;P&gt;OUTPUT&lt;BR /&gt;
D:\Splunk\bin&amp;gt;splunk cmd python d:\Splunk\bin\scripts\globalapps\google_analytic&lt;BR /&gt;
s_wrapper.py&lt;/P&gt;

&lt;P&gt;D:\Splunk\bin&amp;gt;02/10/14 17:11:37 activeVisitors=889&lt;/P&gt;

&lt;H2&gt;Wrapper code&lt;/H2&gt;

&lt;P&gt;import os, sys&lt;BR /&gt;
import time&lt;/P&gt;

&lt;H3&gt;DELETING ENV VARS&lt;/H3&gt;

&lt;H1&gt;Remove problematic environmental variables if they exist.&lt;/H1&gt;

&lt;P&gt;for envvar in ("PYTHONPATH", "LD_LIBRARY_PATH"):&lt;BR /&gt;
    if envvar in os.environ:&lt;BR /&gt;
        del os.environ[envvar]&lt;/P&gt;

&lt;H3&gt;USING OS INTERPRETER&lt;/H3&gt;

&lt;P&gt;python_executable = "C:\Python27\python.exe"&lt;/P&gt;

&lt;P&gt;real_script = os.path.join(os.path.dirname(&lt;STRONG&gt;file&lt;/STRONG&gt;), "real_time_analytics.py")&lt;/P&gt;

&lt;H2&gt;os.execv(python_executable, [ python_executable, real_script ] )&lt;/H2&gt;</description>
      <pubDate>Mon, 28 Sep 2020 15:51:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Help-with-python-wrapper/m-p/144905#M2007</guid>
      <dc:creator>jedatt01</dc:creator>
      <dc:date>2020-09-28T15:51:21Z</dc:date>
    </item>
    <item>
      <title>Re: Help with python wrapper!</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Help-with-python-wrapper/m-p/144906#M2008</link>
      <description>&lt;P&gt;Short Answer:  Can you use &lt;CODE&gt;subprocess.call([python_executable, real_script])&lt;/CODE&gt; instead?&lt;/P&gt;

&lt;P&gt;I confirmed that the script appears to do the right thing under OSX, so I dug out my Windows laptop and confirmed that it appears to fork and thus the wrapper script exits so it wouldn't see real_script's output&lt;/P&gt;

&lt;P&gt;In researching msg 109179 on issue9148 (sorry it says I don't have enough karma to link to an external site): "on Windows, exec() does not really replace the current process. It creates a new process (with a new pid), and exits the current one"  Thus this appears to be due to the way python behaves on windows.&lt;/P&gt;

&lt;P&gt;I was able to get around the difference by using subprocess.call (I tested having python 2.7 call python 3.3 and printing the versions in both to confirm my understanding of the desired behavior.&lt;/P&gt;

&lt;P&gt;Edit: As I processed things overnight, I realized I didn't point out that subprocess.call &lt;STRONG&gt;is&lt;/STRONG&gt; different; it will fork, run the program, and then return to the original script.  If you put a print statement after the subprocess.call it &lt;STRONG&gt;would&lt;/STRONG&gt; execute the print statement (not tested, but based on my understanding).  I think for your use case, this shouldn't matter since the original script is a wrapper script written explicitly for this purpose.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Feb 2014 04:09:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Help-with-python-wrapper/m-p/144906#M2008</guid>
      <dc:creator>triest</dc:creator>
      <dc:date>2014-02-11T04:09:39Z</dc:date>
    </item>
    <item>
      <title>Re: Help with python wrapper!</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Help-with-python-wrapper/m-p/144907#M2009</link>
      <description>&lt;P&gt;triest, I think i'm almost there. I'm now using subprocess.call and the script output shows correctly when I run in command prompt, but Splunk is still not sucking up the output from the subscript. In your edit you mentioned putting a print statement at the bottom but I couldn't get that to work. If I try to assign the subprocess.call to a variable, it just outputs an integer. Any ideas how to send the output back to the wrapper script for stdoutput?&lt;/P&gt;</description>
      <pubDate>Tue, 11 Feb 2014 14:29:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Help-with-python-wrapper/m-p/144907#M2009</guid>
      <dc:creator>jedatt01</dc:creator>
      <dc:date>2014-02-11T14:29:18Z</dc:date>
    </item>
    <item>
      <title>Re: Help with python wrapper!</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Help-with-python-wrapper/m-p/144908#M2010</link>
      <description>&lt;P&gt;The only thought I have off the top of my head would be to try:&lt;BR /&gt;
subprocess.call([python_executable, real_script], stdout=sys.stdout)&lt;/P&gt;

&lt;P&gt;I'm at work so I only have my Mac with me and on it that doesn't appear to change anything.  I've not played with scripted inputs much yet so there may be some subtle aspect I'm not thinking of.  I'll check back tonight (probably 9:00pm EST) and if there isn't anything I'll play with it for a couple of hours tonight and see if I can come up with something. &lt;/P&gt;

&lt;P&gt;If you find a solution or workaround, please let me know!&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 15:51:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Help-with-python-wrapper/m-p/144908#M2010</guid>
      <dc:creator>triest</dc:creator>
      <dc:date>2020-09-28T15:51:58Z</dc:date>
    </item>
    <item>
      <title>Re: Help with python wrapper!</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Help-with-python-wrapper/m-p/144909#M2011</link>
      <description>&lt;P&gt;well, turns out your first solution did work! I had some problems with my splunk instance and had to reinstall. Just to be thorough, i'll add that you need to import the subprocess module at the top of the wrapper script for this to work. Thanks for the help!&lt;/P&gt;</description>
      <pubDate>Tue, 11 Feb 2014 18:27:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Help-with-python-wrapper/m-p/144909#M2011</guid>
      <dc:creator>jedatt01</dc:creator>
      <dc:date>2014-02-11T18:27:56Z</dc:date>
    </item>
  </channel>
</rss>

