Can't turn a python script into a module, how can i get his stdout in a View ?

Hello,
Sherlock (github link) is a python script which takes arguments :

python3 sherlock user1 user2 user3

I can’t turn it into a module easily by adding an __init__.py file in the script folder. It’s not callable.

I would like to get his stdout (a list of urls displayed in terminal) in a View. Do i need subprocess module to run it into a view ? Is there another way ?

I know there’s a big security problem by asking users arguments for a command line in a web form. How can i secure it ?

Thanks a lot.

Yes, to capture stdout you either need to run it as a subprocess or reassign stdout to a pipe that you can manage.

Without knowing the number, type, and purpose of the arguments, there’s no way to evaluate the security involved with passing them as arguments. In the most general case, you just need to ensure that those parameters are appropriate.

And without knowing the script, it’s hard to tell whether or not it can be turned into a module or a callable.

Basically, we would need to know a lot more about this script in order to provide any useful and specific advice.

I edited the original post to make the topic clearer for readers.

If you look at the __main__.py file in the project, you will see the following lines at the bottom of the script:

    import sherlock
    sherlock.main()

This wrapper script does import sherlock as a module, invoking the main method.

But, there’s a problem within the script if you were to try and use it that way. Scattered throughout the code are numerous sys.exit calls used to exit the methods. That’s something you don’t want to have happen within your view.

So in this particular case, yes. You will need to run this as a subprocess.

1 Like