Code to send message from Python script to FPC's TSimpleIPCServer

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Code to send message from Python script to FPC's TSimpleIPCServer

Michalis Kamburelis-3
Just sharing, in case someone will find the below information useful,
or worthy to add to some wiki or docs anywhere:

I have here a daemon written in FPC that communicates with clients by
very nice TSimpleIPCServer class (in SimpleIPC unit). Of course it's
trivial to send a message to such server from another FPC program (see
fcl-base/examples/ipcclient.pp). Turns out it's also trivial to send
a message from a Python program. Attaching the Python code. You can
probably easily add alternatives for any other programming language to
this :)

Note that this is specific to Unix, where SimpleIPC just uses a named
pipe, so it looks just like a writeable file (/tmp/server_name) to any
other process. I don't know how it looks for other OSes.



# Test that you can write from Python to ipcserver named pipe.
# FPC handles messages by very nice and comfortable FPC unit simpleipc.
# See packages/fcl-process/src/unix/
# Message send/received is represented by
# TMsgHeader = Packed record
#   Version : Byte; // always MsgVersion = 1
#   MsgType : TMessageType; // actually anything, server ignores it,
#   // but convention says SendStringMessage sets MsgType = mtString = 1.
#   // TMessageType is just LongInt.
#   MsgLen  : Integer; // length in bytes of following data
# end;
# FPC hides this from ipcserver, but in Python program we have to be able
# to encode such thing "by hand".

import struct

def send_to_daemon(s):
    f = open('/tmp/ipcserver', 'wb')
    f.write(struct.pack('b', 1) +
            struct.pack('l', 1) +
            struct.pack('l', len(s)) +

send_to_daemon('Testing messad asd asd asdf asdfa sdf 123')

fpc-pascal maillist  -  [hidden email]