|
1 | | -#!/usr/bin/env python |
| 1 | +#!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import sys |
4 | 4 | import json |
5 | 5 | import struct |
6 | 6 |
|
7 | | -try: |
8 | | - # Python 3.x version |
9 | | - # Read a message from stdin and decode it. |
10 | | - def getMessage(): |
11 | | - rawLength = sys.stdin.buffer.read(4) |
12 | | - if len(rawLength) == 0: |
13 | | - sys.exit(0) |
14 | | - messageLength = struct.unpack('@I', rawLength)[0] |
15 | | - message = sys.stdin.buffer.read(messageLength).decode('utf-8') |
16 | | - return json.loads(message) |
17 | | - |
18 | | - # Encode a message for transmission, |
19 | | - # given its content. |
20 | | - def encodeMessage(messageContent): |
21 | | - # https://docs.python.org/3/library/json.html#basic-usage |
22 | | - # To get the most compact JSON representation, you should specify |
23 | | - # (',', ':') to eliminate whitespace. |
24 | | - # We want the most compact representation because the browser rejects |
25 | | - # messages that exceed 1 MB. |
26 | | - encodedContent = json.dumps(messageContent, separators=(',', ':')).encode('utf-8') |
27 | | - encodedLength = struct.pack('@I', len(encodedContent)) |
28 | | - return {'length': encodedLength, 'content': encodedContent} |
29 | | - |
30 | | - # Send an encoded message to stdout |
31 | | - def sendMessage(encodedMessage): |
32 | | - sys.stdout.buffer.write(encodedMessage['length']) |
33 | | - sys.stdout.buffer.write(encodedMessage['content']) |
34 | | - sys.stdout.buffer.flush() |
35 | | - |
36 | | - while True: |
37 | | - receivedMessage = getMessage() |
38 | | - if receivedMessage == "ping": |
39 | | - sendMessage(encodeMessage("pong3")) |
40 | | -except AttributeError: |
41 | | - # Python 2.x version (if sys.stdin.buffer is not defined) |
42 | | - # Read a message from stdin and decode it. |
43 | | - def getMessage(): |
44 | | - rawLength = sys.stdin.read(4) |
45 | | - if len(rawLength) == 0: |
46 | | - sys.exit(0) |
47 | | - messageLength = struct.unpack('@I', rawLength)[0] |
48 | | - message = sys.stdin.read(messageLength) |
49 | | - return json.loads(message) |
50 | | - |
51 | | - # Encode a message for transmission, |
52 | | - # given its content. |
53 | | - def encodeMessage(messageContent): |
54 | | - # https://docs.python.org/3/library/json.html#basic-usage |
55 | | - # To get the most compact JSON representation, you should specify |
56 | | - # (',', ':') to eliminate whitespace. |
57 | | - # We want the most compact representation because the browser rejects |
58 | | - # messages that exceed 1 MB. |
59 | | - encodedContent = json.dumps(messageContent, separators=(',', ':')) |
60 | | - encodedLength = struct.pack('@I', len(encodedContent)) |
61 | | - return {'length': encodedLength, 'content': encodedContent} |
62 | | - |
63 | | - # Send an encoded message to stdout |
64 | | - def sendMessage(encodedMessage): |
65 | | - sys.stdout.write(encodedMessage['length']) |
66 | | - sys.stdout.write(encodedMessage['content']) |
67 | | - sys.stdout.flush() |
68 | | - |
69 | | - while True: |
70 | | - receivedMessage = getMessage() |
71 | | - if receivedMessage == "ping": |
72 | | - sendMessage(encodeMessage("pong2")) |
| 7 | +# Read a message from stdin and decode it. |
| 8 | +def getMessage(): |
| 9 | + rawLength = sys.stdin.buffer.read(4) |
| 10 | + if len(rawLength) == 0: |
| 11 | + sys.exit(0) |
| 12 | + messageLength = struct.unpack('@I', rawLength)[0] |
| 13 | + message = sys.stdin.buffer.read(messageLength).decode('utf-8') |
| 14 | + return json.loads(message) |
| 15 | + |
| 16 | +# Encode a message for transmission, given its content. |
| 17 | +def encodeMessage(messageContent): |
| 18 | + # https://docs.python.org/3/library/json.html#basic-usage |
| 19 | + # To get the most compact JSON representation, you should specify |
| 20 | + # (',', ':') to eliminate whitespace. |
| 21 | + # We want the most compact representation because the browser rejects |
| 22 | + # messages that exceed 1 MB. |
| 23 | + encodedContent = json.dumps(messageContent, separators=(',', ':')).encode('utf-8') |
| 24 | + encodedLength = struct.pack('@I', len(encodedContent)) |
| 25 | + return {'length': encodedLength, 'content': encodedContent} |
| 26 | + |
| 27 | +# Send an encoded message to stdout |
| 28 | +def sendMessage(encodedMessage): |
| 29 | + sys.stdout.buffer.write(encodedMessage['length']) |
| 30 | + sys.stdout.buffer.write(encodedMessage['content']) |
| 31 | + sys.stdout.buffer.flush() |
| 32 | + |
| 33 | +while True: |
| 34 | + receivedMessage = getMessage() |
| 35 | + if receivedMessage == "ping": |
| 36 | + sendMessage(encodeMessage("pong")) |
0 commit comments