Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send commands from command line to a running Python script in Unix

I want to build a script that can be controlled while running from another scripts. For example I want to run my script like this:

~: Server &

and be able to run one of it's functions like:

~: client func1

Upon my searches I find signal module that have something like I want, but it's signals are predefined and I can not send signals of my own.

I even though of a client/server implementation using a network framework but I think it's too much for the abilities that I want my script to have.

Thank you all.

like image 601
Shahinism Avatar asked Apr 18 '14 05:04

Shahinism


People also ask

How do I call a Python script from the command line?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

How do you call a Python command in Linux?

The second way to run Linux commands with Python is by using the newer subprocess module. This module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. It was created to replace both os.

How do you pass arguments to function while executing a Python script from terminal?

You can use the command line arguments by using the sys. argv[] array. The first index of the array consists of the python script file name. And from the second position, you'll have the command line arguments passed while running the python script.


2 Answers

So you need to talk to a running process right? What about the idea of unix domain socket?

In your server you can build up a socket on a path of your unix file system, then talk to that socket in you client, like described in this link I googled:

http://pymotw.com/2/socket/uds.html

like image 37
oodograss Avatar answered Oct 20 '22 07:10

oodograss


If you are only trying to send commands one-directionally to a server, it is easier than you think, using Python's Sockets. The code samples are of course barebones in the sense that they do not do error handling, and do not call recv multiple times to make sure the message is complete. This is just to give you an idea of how few lines of code it takes to process commands.

Here is a server program that simply receives messages and prints to stdout. Note that we use threading so that the server can listen to multiple clients at once.

import socket
from threading import Thread


MAX_LENGTH = 4096

def handle(clientsocket):
  while 1:
    buf = clientsocket.recv(MAX_LENGTH)
    if buf == '': return #client terminated connection
    print buf

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

PORT = 10000
HOST = '127.0.0.1'

serversocket.bind((HOST, PORT))
serversocket.listen(10)

while 1:
    #accept connections from outside
    (clientsocket, address) = serversocket.accept()

    ct = Thread(target=handle, args=(clientsocket,))
    ct.start()

Here is a client program that sends commands to it.

import socket
import sys


HOST = '127.0.0.1'
PORT = 10000
s = socket.socket()
s.connect((HOST, PORT))

while 1:
    msg = raw_input("Command To Send: ")
    if msg == "close":
       s.close()
       sys.exit(0)
    s.send(msg)
like image 91
merlin2011 Avatar answered Oct 20 '22 07:10

merlin2011