Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZeroNet REST API #1137

Closed
filips123 opened this issue Oct 8, 2017 · 9 comments
Closed

ZeroNet REST API #1137

filips123 opened this issue Oct 8, 2017 · 9 comments

Comments

@filips123
Copy link
Contributor

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

@HelloZeroNet
Copy link
Owner

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

@filips123
Copy link
Contributor Author

filips123 commented Oct 10, 2017

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

@HelloZeroNet
Copy link
Owner

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", ...

@filips123
Copy link
Contributor Author

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

@HelloZeroNet
Copy link
Owner

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

@filips123
Copy link
Contributor Author

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

@mkg20001
Copy link
Contributor

@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)

@HelloZeroNet
Copy link
Owner

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

@filips123
Copy link
Contributor Author

It works. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants