ZeroNet REST API

Created on 8 Oct 2017  路  9Comments  路  Source: HelloZeroNet/ZeroNet

Can I use a ZeroNet for decentralized REST API server and client? If so, how? If not, can you enable this?

All 9 comments

There is no REST API, but ZeroNet using Websocket for API requests/responses/events.

How can I use Websocket? I have a Python program and I would like to communicate with ZeroNet site.

Here is a simple example using websocket-client:

import urllib2
import re
import json
import time

import websocket

req = urllib2.Request("http://127.0.0.1:43110/1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D/", headers={"Accept" : "text/html"})
wrapper_body = urllib2.urlopen(req).read()
wrapper_key = re.search('wrapper_key = "(.*?)"', wrapper_body).group(1)

ws = websocket.create_connection("ws://127.0.0.1:43110/Websocket?wrapper_key=%s" % wrapper_key)
ws.send(json.dumps({"cmd":"siteInfo","params":{},"id":1000001}))
res = ws.recv()
print res
# {"to": 1000001, "cmd": "response", "result": {"tasks": 0, "size_limit": 10, "address": "1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D", ...

If I have a javascript function on the ZeroNet page, how can I call it from Python and get a function return?

You need a headless browser to do that, eg.: http://phantomjs.org/

If I understand correctly, I need to run PhantomJS from Python, which sends a request to the ZeroNet page. Is there any other way?

@filips123 nodeJS and using a module like jsdom or directly using regex and a websocket module in python (this would require rewriting that particular function to python)

I think PhantomJS is the easiest way, here is an example: https://github.com/HelloZeroNet/ZeroMail/blob/master/echobot/echobot.py

It works. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HelloZeroNet picture HelloZeroNet  路  40Comments

HelloZeroNet picture HelloZeroNet  路  36Comments

HelloZeroNet picture HelloZeroNet  路  45Comments

HelloZeroNet picture HelloZeroNet  路  41Comments

HelloZeroNet picture HelloZeroNet  路  36Comments