A word about subprocess module for running bash commands in Python
June 21, 2017 · 5 mins read
It has been a few days since I have been working on the GSoC project. Although I haven’t been able to write about all the things going on that side. From now on probably I will talk about it more often. Yesterday I managed to upload the code on our local experimental server and show the results to the mentor. Thank god, it didn’t break( I haven’t written any code to handle the errors) and everything worked as expected. Next thing is to attach celery so that the actions happens in the background. Now both celery thing and the server are for the other day. Today I will be talking about subprocess, a module of python that we are going to use for running bash commands. What we have done till now: We have made the models which were a hectic task because we don’t really know what was coming out of the scans. After that, I made two main views to start with. One for scanning URL and other for getting files from the local. Today we are going to talk about using subprocess module to run bash commands in a python program. According to python docs, subprocess can be used as an alternative for the following purposes.
We used the module for our own purpose. We used the Django to upload the file to the server using the basic FileField type in the form. Everything about that task is written in the another post which you will find under the GSoC-2017 category. After the file was uploaded we wanted to apply some bash commands for scanning the code. I was thinking of placing the scancode-toolkit code in the root directory of the project then in the bash script, we will write the following initial commands.
os.system os.spawn* os.popen* popen2.* commands.*
$ cd ../scancode-toolkit/As we are in the directory inside the scancode-server. This will give us the opportunity to scan the code using the following command.
$ ./scancode file_nameThen the mentor said that we can directly install scancode using the following command.
$ pip install scancode-toolkitThis changed my view and now I was going to simply put the scancode-toolkit into the requirements.txt file. After that, I was going to import subprocess module and write a few lines of code that can scan the code. Following is the commit in which I wrote the code to do so. https://github.com/nexB/scancode-server/commit/d1431724cb5241c56f3e1a7f507cf854b919af24 In the code, I haven’t written anything to handle errors. In the next few commits, we will concentrate on doing so. The rest part of the post will discuss some other features of the subprocess module. subprocess.call is frequently used in the case of subprocess module. A simple implementation of the subprocess is as follows:
The result will be shown as standard output to the command line. In subprocess, we have an option to call a program by executing it like the shell does it. For that, we use the following code.
>>> import subprocess >>> subprocess.call(['ls', '-l'])
For getting more information read. http://sharats.me/the-ever-useful-and-neat-subprocess-module.html How to
>>> subprocess.call('ls -l', shell=True)
PIPEthe result of one module to next module: http://kendriu.com/how-to-use-pipes-in-python-subprocesspopen-objects
Or an alternative of
grep = Popen('grep ntp'.split(), stdin=PIPE, stdout=PIPE) ls = Popen('ls'.split(), stdout=grep.stdin) output = grep.communicate() ls.wait()
communicate()is to use.
That’s it for today. We will come back with more exciting things tomorrow.
>>> grep.wait() >>> print grep.stdout.read()
Please share your Feedback:
Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!