#!/usr/bin/env python
import json
import struct
import sys


# Read a message from stdin and decode it.
def getMessage():
    rawLength = sys.stdin.read(4)
    if len(rawLength) == 0:
        sys.exit(0)
    messageLength = struct.unpack('@I', rawLength)[0]
    message = sys.stdin.read(messageLength)
    return json.loads(message)


# Encode a message for transmission,
# given its content.
def encodeMessage(messageContent):
    encodedContent = json.dumps(messageContent)
    encodedLength = struct.pack('@I', len(encodedContent))
    return {'length': encodedLength, 'content': encodedContent}


# Send an encoded message to stdout
def sendMessage(encodedMessage):
    sys.stdout.write(encodedMessage['length'])
    sys.stdout.write(encodedMessage['content'])
    sys.stdout.flush()

while True:
    receivedMessage = getMessage()
    if receivedMessage == 'ping':
        sendMessage(encodeMessage('pong'))

@@ -14,16 +14,28 @@
 v python
+%0A%0Aimport sys
 %0Aimport 
@@ -57,21 +57,1138 @@
 uct%0A
-import sys%0A%0A%0A
+%0Atry:%0A    # Python 3.x version%0A    # Read a message from stdin and decode it.%0A    def getMessage():%0A        rawLength = sys.stdin.buffer.read(4)%0A        if len(rawLength) == 0:%0A            sys.exit(0)%0A        messageLength = struct.unpack('@I', rawLength)%5B0%5D%0A        message = sys.stdin.buffer.read(messageLength).decode('utf-8')%0A        return json.loads(message)%0A%0A    # Encode a message for transmission,%0A    # given its content.%0A    def encodeMessage(messageContent):%0A        encodedContent = json.dumps(messageContent).encode('utf-8')%0A        encodedLength = struct.pack('@I', len(encodedContent))%0A        return %7B'length': encodedLength, 'content': encodedContent%7D%0A%0A    # Send an encoded message to stdout%0A    def sendMessage(encodedMessage):%0A        sys.stdout.buffer.write(encodedMessage%5B'length'%5D)%0A        sys.stdout.buffer.write(encodedMessage%5B'content'%5D)%0A        sys.stdout.buffer.flush()%0A%0A    while True:%0A        receivedMessage = getMessage()%0A        if receivedMessage == %22ping%22:%0A            sendMessage(encodeMessage(%22pong3%22))%0Aexcept AttributeError:%0A    # Python 2.x version (if sys.stdin.buffer is not defined)%0A    
 # Re
@@ -1226,16 +1226,20 @@
 ode it.%0A
+    
 def getM
@@ -1244,24 +1244,28 @@
 tMessage():%0A
+    
     rawLengt
@@ -1282,24 +1282,28 @@
 din.read(4)%0A
+    
     if len(r
@@ -1318,32 +1318,36 @@
 ) == 0:%0A        
+  
+  
 sys.exit(0)%0A    
@@ -1338,24 +1338,28 @@
 sys.exit(0)%0A
+    
     messageL
@@ -1400,24 +1400,28 @@
 gth)%5B0%5D%0A    
+   
+ 
 message = sy
@@ -1448,16 +1448,20 @@
 Length)%0A
+    
     retu
@@ -1484,17 +1484,20 @@
 ssage)%0A%0A
-%0A
+    
 # Encode
@@ -1525,16 +1525,20 @@
 ission,%0A
+    
 # given 
@@ -1550,16 +1550,20 @@
 ontent.%0A
+    
 def enco
@@ -1589,24 +1589,28 @@
 ntent):%0A    
+ 
+   
 encodedConte
@@ -1641,16 +1641,20 @@
 ontent)%0A
+    
     enco
@@ -1704,24 +1704,28 @@
 ntent))%0A    
+   
+ 
 return %7B'len
@@ -1773,17 +1773,20 @@
 ntent%7D%0A%0A
-%0A
+    
 # Send a
@@ -1813,16 +1813,20 @@
  stdout%0A
+    
 def send
@@ -1846,24 +1846,28 @@
 edMessage):%0A
+    
     sys.stdo
@@ -1897,24 +1897,28 @@
 %5B'length'%5D)%0A
+    
     sys.stdo
@@ -1953,24 +1953,28 @@
 tent'%5D)%0A    
+ 
+   
 sys.stdout.f
@@ -1981,16 +1981,20 @@
 lush()%0A%0A
+    
 while Tr
@@ -1997,24 +1997,28 @@
 e True:%0A    
+  
+  
 receivedMess
@@ -2032,24 +2032,28 @@
 etMessage()%0A
+    
     if recei
@@ -2070,14 +2070,14 @@
  == 
-'
+%22
 ping
-'
+%22
 :%0A  
@@ -2074,32 +2074,36 @@
 %22ping%22:%0A        
+    
 sendMessage(enco
@@ -2116,13 +2116,14 @@
 age(
-'
+%22
 pong
-'
+2%22
 ))%0A

Fix native message examples with python3 (#157)