Domanda

I would like to stream a JSON locally with python (so as another program read it). Is there any package that streams in a clean way the json in a local address? (as I used print but instead of the terminal, a local url).

Thanks

È stato utile?

Soluzione

This should do it:

import SocketServer
import json

class Server(SocketServer.ThreadingTCPServer):
    allow_reuse_address = True

class Handler(SocketServer.BaseRequestHandler):
    def handle(self):
        self.request.sendall(json.dumps({'id':'3000'}))  # your JSON

server = Server(('127.0.0.1', 50009), Handler)
server.serve_forever()

Test with:

~  ᐅ curl 127.0.0.1:50009
{"id": 3000}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top