Call Python Script from Ruby

Call python script from ruby

One way would be exec.

result = exec("python script.py params")

How to Run a Python Script from a Rails Application?

Here are ways to execute a shell script

`python pythonscript.py`

or

system( "python pythonscript.py" )

or

exec(" python pythonscript.py")

exec replaces the current process by running the given external command.

Returns none, the current process is replaced and never continues.

Run a Ruby Script with Python

Most probably, ruby in your python script is not the same as ruby in your shell. This can happen if you e.g. have a default system ruby installed (e.g. from the system packages) and you install another ruby version via RVM.

When using shell, the RVM automatically loads it's environment from the shell init scripts (e.g. ~/.bashrc) and knows which ruby to use from its settings. Whereas your python script does not load any rvm environment (it's not a login shell) and calls the default system ruby.

In that case, you need to explicitly call the correct ruby from the RVM in your python script. You can to it by calling the RVM wrapper:

  • browse directories under ~/.rvm/wrappers/ and find the correct ruby version and gemset that you want to use
  • in your python script, call the ruby command from this wrapper directory instead of the plain `ruby, something like:
rvm_ruby = os.environ['HOME'] + "/.rvm/wrappers/ruby-2.3.0-p100@myproject/ruby"
os.system(rvm_ruby + " ./wpscan.rb -u www.mysite.com")

This should fix your problem.

Ruby: get return value of python script

First, have your Python script print the value rather than returning it, or add print main() at the bottom so the return value of main() gets printed.

Second, on the Ruby side, execute it with backticks rather than the system() function, like this:

output = `python script.py`

This captures the output of the Python script as a string. If you want it as a Ruby array, you'll need to parse it. Ruby's array literal syntax is similar to Python's list literal syntax, so this is not as tough as it might seem. If you can find something that parses strings into Ruby arrays (besides eval() because it's dangerous) you should be covered. Problems will arise when you have things besides simple types, None, or potentially strings with escapes in them.

I am more a Python guy than a Ruby guy, but Ruby doesn't seem to have anything like Python's ast.literal_eval (a safe eval() that only accepts literals) in its standard library. However, I did find parsr which appears to be exactly that.

If the Python list literals you're getting aren't valid Ruby, you can use JSON as the interchange format:

# Python side
import json, sys
json.dump(main(), sys.stdout)

# Ruby side
require 'json'
output = JSON.parse(`python script.py`)

Calling Python from Ruby

This article gives some techniques for running Ruby code from Python which should also be applicable in the reverse direction (such as XML-RPC or pipes) as well as specific techniques for running Python code from Ruby. In particular rubypython or Ruby/Python look like they may do what you want.

Call python code from Ruby

RubyPython allows you to use Python objects from within Ruby, but your code has to be valid Ruby code (it just exposes wrapper objects that work with the Python objects in the background). You could translate your code into the Ruby equivalent using this tool, but at that point, you might as well just rewrite it in Ruby, which would be cleaner, more readable, and more efficient.

There is no way, as far as I know, to insert Python code into a Ruby script and have it run. Such a project is pretty out-there and unlikely to be anything approaching efficient.

Either rewrite your Python code as Ruby code, or make your Python code a stand-alone script that you can then call the Python interpreter to run it from Ruby. This is naturally only an option if your code doesn't need a ton of interaction with the rest of your application, as all communication will have to happen using stdin/stdout.

Execute a python script from a Ruby on Rails webserver

There are several ways to do this. The most obvious one would be to write another web service, this one in Python, but that's hardly elegant. Another solution:

Install rubypython gem.

Then you can do things like:

require 'rubypython'
RubyPython.start
my_python_module = RubyPython.import('my.python.module')
value = my_python_module.my_python_function().rubify
RubyPython.stop

You can see more at rubypython documentation pages.

A third option would be to simply shell out and execute Python as any executable, through IO.popen.



Related Topics



Leave a reply



Submit