문제

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

도움이 되었습니까?

해결책

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}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top