I have a website that is backed by CherryPy web framework and a scala program that runs on a same machine and contains an actor system inside. OS is Ubuntu 12.04.
What I want is this: once user fills out and submits a form I send the data from CherryPy backend to scala program as a JSON string. How can this be done? What should I use in my python and scala programs to implement this functionality?
Instead of using raw sockets, you could consider a message broker like RabbitMQ. It supports both Scala and Python.
http://www.rabbitmq.com/tutorials/tutorial-one-python.html
On the Scala side, Akka has an AMQP module which abstracts AMQP Connection, Producer and Consumer as Actors.
http://doc.akka.io/docs/akka-modules/1.3.1/modules/amqp.html
As you are on a *nix system, you may want to look into Unix domain sockets (that link contains a very clear example use).
You can use the python socket module to easily create a Unix socket using:
import socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
Related
Will gRPC support in Python allow me to implement a server that listens on a Unix domain socket (as opposed to a port)? I am using Python 3.5.3 and grpcio/grpcio-tools 1.18.0.
So far, I have not been able to find any relevant example nor direct evidence. The official examples use server.add_insecure_port('[::]:50051'), and its not clear how a socket could fit there.
Apparently add_insecure_port(address) accepts Unix domain sockets in the format unix://var/run/test.sock after all.
I would like to have a server process (preferably Python) that accepts simple messages and multiple clients (again, preferably Python) that connect to the server and send messages to it. The server and clients will only ever be running on the same local machine and the OS is Linux based. The server will be automatically started by the OS and the clients started later independent of the server. I strongly want to avoid installing a whole separate messaging framework/server to do this. The messages will be simple strings such as "kick" or even just a single byte representing the message type. It also needs to know when a connection is made and lost.
From these requirements, I think named pipes would be a feasible solution, with a new instance of that pipe created for each client connection. However, when I search for examples, all of the ones I have come across deal with processes that are spawned from the same parent process and not independently started which means they can pass a parent reference to the child.
Windows seems to allow multiple instances of a named pipe (one for each client connection), but I'm unsure if this is possible on a Linux based OS?
Please could someone point me in the right direction, preferably with a basic example, even if it's just pseudo-code.
I've looked at the multiprocessing module in Python, but this seems to be oriented around the server and client sharing the same process or having one spawn the other.
Edit
May be important, the host device is not guaranteed to have networking capabilities (embedded device).
I've used zeromq for this sort of thing before. it's a relatively lightweight library that exposes this sort of functionality
otherwise, you could implement it yourself by binding a socket in the server process and having clients connect to it. this works fine for unix domain sockets, just pass AF_UNIX when creating the socket, e.g:
import socket
with socket.socket(socket.AF_UNIX) as s:
s.bind('/tmp/srv')
s.listen(1)
(c, addr) = s.accept()
with c:
c.send(b"hello world")
for the server, and:
with socket.socket(socket.AF_UNIX) as c:
c.connect('/tmp/srv')
print(c.recv(8192))
for the client.
writing a protocol around this is more involved, which is where things like zmq really help where you can easily push JSON messages around
Is it possible to use Django for communication with some kind of server process? For example on my Django website I want to have form where I input connection details (host and port) and after connection I want to send some request or events to other server process (some simple action like slider moving or clicking a button). Can I use python socket programming for this or is there some easier way?
You can use with django any python packages as with any "normal" python program. If you have a module, that communicate with your server, you can use this, if not, you have to write one on your own possibly with socket programming.
I'm working on a project that needs a framework to handle pub/sub connections between a webpage and Python.
I've already used mosquitto (an open-source implementation of MQTT) and it worked, but the server needs a modded Apache module to redirect WebSocket connections to the broker.
Right now, I'm looking at Tornado but it doesn't fit on my requirements. I need a solution for the follwing:
A web page connects to a python server or some kind of broker and subscribes a topic do receive data associated with that topic.
Every time Python has data associated with that topic (let's say every 10 seconds), the data is sent to the specific client (or clients) that subscribed to that topic.
Thanks in advance
You could try the HiveMQ* MQTT broker instead of mosquitto as that has MQTT over websocket support built in.
http://www.hivemq.com/
Autobahn provides Publish & Subscribe (and RPC) over WebSocket via the WAMP protocol, and comes with client for JS (besides others) and Python/Twisted for server.
Here is a complete example.
Disclosure: I am original author of Autobahn and work for Tavendo.
websockify provides a websockets to tcp proxy that you could put in front of mosquitto. You would have to run it on a different port than 80 if you already have a web server of course, but it is easier than dealing with custom apache/lighttpd modules.
The situation:
I have a python script to connect/send signals to serial connected arduino's. I wanted to know the best way to implement a web server, so that i can query the status of the arduinos. I want that both the "web server" part and serial connection runs on the same script. Is it possible, or do i have to break it into a daemon and a server part?
Thanks, any comments are the most welcomed.
Have WAMP server. It is the easiest and quickest way. The web server will support php, python , http etc.
If you are using Linux , the easiest tool for serial communication is php.
But in windows php cannot read data from serial communication. Hence use python / perl etc.
Thanks
For those wondering what I have opted for; I have decoupled the two part:
The Arduino daemon
I am using Python with a micro web framework called [Bottle][1] which handles the API calls and I have used PySerial to communicate with the Arduino's.
The web server
The canonical Apache and PHP; are used to make API calls to the Arduino daemon.