Communication between two python scripts Communication between two python scripts python python

Communication between two python scripts


zeromq: http://www.zeromq.org/ - is best solution for interprocess communications imho and have a excelent binding for python: http://www.zeromq.org/bindings:python


Since the "main" script looks like a service you can enhance it with a web API. bottle is the perfect solution for this. With this additional code your python script is able to receive requests and process them:

import jsonfrom bottle import run, post, request, response@post('/process')def my_process():  req_obj = json.loads(request.body.read())  # do something with req_obj  # ...  return 'All done'run(host='localhost', port=8080, debug=True)

The client script may use the httplib to send a message to the server and read the response:

import httplibc = httplib.HTTPConnection('localhost', 8080)c.request('POST', '/process', '{}')doc = c.getresponse().read()print doc# 'All done'


In case you are interested in implementing the client script that Mike presented in Python 3.x, you will quickly find that there is no httplib available. Fortunately, the same thing is done with the library http.client.

Otherwise it is the same:

import http.clientc = http.client.HTTPConnection('localhost', 8080)c.request('POST', '/process', '{}')doc = c.getresponse().read()print(doc)

Though this is old I would figure I would post this since I had a similar question today but using a server.