0

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

Diolor
  • 13,181
  • 30
  • 111
  • 179
  • what do you mean "stream json"? – Joran Beasley Aug 16 '13 at 22:18
  • @JoranBeasley I believe he is referring to a way to make a JSON available to multiple process on the machine. – Mathieu Marques Aug 16 '13 at 22:23
  • if you have a file open for reading other processes should be able to open it fine ... – Joran Beasley Aug 16 '13 at 22:24
  • @JoranBeasley ... or through a [socket](http://docs.python.org/2/library/socket.html)? – Sylvain Leroux Aug 16 '13 at 22:29
  • I have a listener (?) in my Gephi application ready to get information as json, so if I am correct I need to push the json on a local address in order the listener get the data. Maybe creating a small local server? – Diolor Aug 16 '13 at 22:30
  • Not sure if this is what you want, but as a workaround to letting javascript access my local json, I just kind of embed my json inside a javascript and then include that javascript in my web page. – Penguinator Aug 16 '13 at 22:32
  • maybe this is what your looking for? http://docs.paasmaker.org/user-gettingstarted.html .... I dunno its hard to tell what you want to do .. – Joran Beasley Aug 16 '13 at 22:40

1 Answers1

1

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}
elyase
  • 39,479
  • 12
  • 112
  • 119