Skip to content

Instantly share code, notes, and snippets.

@crast
Last active January 25, 2017 22:56
Show Gist options
  • Save crast/e5ad5957a0a86fd41ea0808d8b27ce79 to your computer and use it in GitHub Desktop.
Save crast/e5ad5957a0a86fd41ea0808d8b27ce79 to your computer and use it in GitHub Desktop.
Native program example
import sys
import struct
import socket
import json
class MyApp(object):
def __init__(self):
self.connection = socket.socket(shit I forgot the parameters)
def main():
while True:
# Each message preceded by a 32-bit length
length_encoded = sys.stdin.read(4)
length = struct.unpack('=L', length_encoded)[0]
body_bytes = sys.stdin.read(length)
body = json.loads(body_bytes)
response = self.handle_command(body)
response_encoded = json.dumps(response)
sys.stdout.write(struct.pack('=L', len(response_encoded)))
sys.stdout.write(response_encoded)
def handle_command(self, body):
if body['action'] == 'purchase':
self.connection.send('BUY %s %s %s', body['product_id'], body['cost'], body['credit_card']['cvv'])
return {'purchase': 'OK'}
if body['action'] == 'something':
return {'something': 'something'}
# shit, unrecognized command
return {'status': 'err', 'error': 'wtf, unrecognized command'}
if __name__ == __main__:
myapp = MyApp()
myapp.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment