Skip to content

April 14, 2011

How to send & receive serialized simple objects across PHP & Python? Of course! JSON

by Joe Kuan

I hacked a client & server PHP programs for part of a demo. However, the notorious memory intensive PHP makes my server process swallowing so much memory (the size of sample data and lots of associative arrays) to a point that is impractical to run. So I decided to bite the bullet to port the server side into Python. Then it was fine until it hit me, “How is the PHP client going to talk to Python server side?”

Before the PHP client & server was communicating via serialize and unserialize calls, but no way I am coding a parser and writer or using XML for that. Thank god, it only took me a little while to figure out. Of course, JSON!!!!!!!!!!!!!! That’s what the name for. Silly me.

For the PHP client & server communicates via “{len}\n{serialized string}” where {len} is the length of the {serialized string}.

# Send query
$query = array('StartTime' => ..., 'EndTime' => ..., 'DataType' => ....);
$szStr = serialize($query);
socket_write($sock, strlen($szStr) . "\n");
socket_send($sock, $szStr, strlen($szStr), MSG_EOR);

# Wait for result
socket_read($sock, $buff, PHP_NORMAL_READ);
socket_recv($sock, $szStr, intval($buff), MSG_WAITALL);
$response = unserialize($szStr);

The changes for the PHP client are only to map the serialize and unserialize to json_encode and json_decode respectively.

As for the Python side, since there is no socket read method like PHP_NORMAL_READ mode, therefore only few more steps are required. See SocketServer and json modules for more details.

class ThreadedUnixRequestHandler(SocketServer.BaseRequestHandler):
        def handle(self):
                reqMsg = self.request.recv(64, socket.MSG_PEEK).strip()
                # Split the length\nMsg... first
                lenStr, msg = reqMsg.split("\n", 1)

                if (lenStr.isdigit()):
                        length = int(lenStr) + len(lenStr) + 1
                        jsonStr = self.request.recv(length)
                        lenStr, jsonStr = jsonStr.split("\n", 1)

                        query = json.loads(jsonStr)
                        # Resolve the query and generate the result ....

                        resultStr = json.dumps(result)
                        lenVal = len(resultStr)
                        lenStr = "%d\n" % len(resultStr)
                        self.request.send(lenStr)
                        self.request.send(resultStr)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: