Skip to content
Snippets Groups Projects
Commit 215d3d28 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

http server: add cmd arguments

parent 43a345ba
No related branches found
No related tags found
No related merge requests found
......@@ -17,6 +17,7 @@
from twisted.web import server, resource
from twisted.internet import reactor, endpoints
from urllib.parse import urlparse
import opendht as dht
import base64, json
......@@ -25,14 +26,15 @@ class DhtServer(resource.Resource):
isLeaf = True
node = dht.DhtRunner()
def __init__(self):
self.node.run()
self.node.bootstrap("bootstrap.ring.cx", "4222")
def __init__(self, port, bootstrap):
self.node.run(port=port)
b_url = urlparse('//'+bootstrap)
self.node.bootstrap(b_url.hostname, str(b_url.port) if b_url.port else '4222')
def render_GET(self, req):
uri = req.uri[1:]
h = dht.InfoHash(uri) if len(uri) == 40 else dht.InfoHash.get(uri.decode())
print('GET', h)
print('GET', '"'+uri.decode()+'"', h)
res = self.node.get(h)
req.setHeader(b"content-type", b"application/json")
return json.dumps({'{:x}'.format(v.id):{'base64':base64.b64encode(v.data).decode()} for v in res}).encode()
......@@ -52,5 +54,13 @@ class DhtServer(resource.Resource):
req.setResponseCode(400)
return json.dumps({'success':False, 'error':'no data parameter'}).encode()
endpoints.serverFromString(reactor, "tcp:8080").listen(server.Site(DhtServer()))
reactor.run()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Launch an OpenDHT node with an HTTP control interface')
parser.add_argument('-p', '--port', help='OpenDHT port to bind', type=int, default=4222)
parser.add_argument('-hp', '--http-port', help='HTTP port to bind', type=int, default=8080)
parser.add_argument('-b', '--bootstrap', help='bootstrap address', default="bootstrap.ring.cx:4222")
args = parser.parse_args()
endpoints.serverFromString(reactor, "tcp:"+str(args.http_port)).listen(server.Site(DhtServer(args.port, args.bootstrap)))
reactor.run()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment