daily pastebin goal
90%
SHARE
TWEET

Untitled

a guest Feb 19th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import os, sys, struct
  4.  
  5.  
  6. def read_message():
  7.     text_length_bytes = sys.stdin.buffer.read(4)
  8.  
  9.     if len(text_length_bytes) == 0:
  10.         sys.exit(0)
  11.  
  12.     # Unpack message length as 4 byte integer.
  13.     text_length = struct.unpack('i', text_length_bytes)[0]
  14.  
  15.     # Read the text (JSON object) of the message.
  16.     text = sys.stdin.buffer.read(text_length).decode('utf-8')
  17.     return text
  18.  
  19.  
  20. def send_message(message):
  21.     # Write message size.
  22.     sys.stdout.buffer.write(struct.pack('I', len(message)))
  23.     # Write the message itself.
  24.     sys.stdout.buffer.write(bytes(message,'utf-8'))
  25.     sys.stdout.flush()
  26.  
  27.  
  28. def main(arguments):
  29.     sys.stderr.write("Started echo host\n")
  30.     sys.stderr.flush()
  31.    
  32.     while True:
  33.         message = read_message()
  34.         if message:
  35.             sys.stderr.write("Received message:"+message+"\n")
  36.             sys.stderr.flush()
  37.             send_message(message.strip())
  38.  
  39.  
  40. if __name__ == '__main__':
  41.     sys.exit(main(sys.argv[1:]))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top