Browse Source

i2pctl: remove requests dependency

pull/59/head
l-n-s 6 years ago
parent
commit
c1959c5c73
  1. 44
      scripts/i2pdctl

44
scripts/i2pdctl

@ -1,20 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import json import json
import logging
import pprint import pprint
import argparse import argparse
import datetime import datetime
import ssl
try: import urllib.request
import requests import urllib.parse
except ImportError: import urllib.error
print("Error: requests module is required. apt install python3-requests")
exit(1)
# Disabling annoying warnings
requests.packages.urllib3.disable_warnings(
requests.packages.urllib3.exceptions.InsecureRequestWarning)
INFO_METHODS = { INFO_METHODS = {
"RouterInfo": { "RouterInfo": {
@ -71,6 +64,12 @@ def humanize_time(milliseconds):
"""Seconds to human readable time""" """Seconds to human readable time"""
return str(datetime.timedelta(milliseconds=milliseconds)) return str(datetime.timedelta(milliseconds=milliseconds))
def do_post(url, data):
req = urllib.request.Request(url, data=data.encode())
with urllib.request.urlopen(req, context=ssl._create_unverified_context()) as f:
resp = f.read().decode('utf-8')
return json.loads(resp)
class I2PControl(object): class I2PControl(object):
"""Talk to I2PControl API""" """Talk to I2PControl API"""
@ -83,20 +82,17 @@ class I2PControl(object):
def token(self): def token(self):
"""Cached authentication token""" """Cached authentication token"""
if not self._token: if not self._token:
self._token = requests.post(self.url, self._token = do_post(self.url,
json.dumps({'id': 1, 'method': 'Authenticate', json.dumps({'id': 1, 'method': 'Authenticate',
'params': {'API': 1, 'Password': self.password}, 'params': {'API': 1, 'Password': self.password},
'jsonrpc': '2.0'}), 'jsonrpc': '2.0'}))["result"]["Token"]
verify=False).json()["result"]["Token"]
return self._token return self._token
def request(self, method, params): def request(self, method, params):
"""Execute authenticated request""" """Execute authenticated request"""
return requests.post(self.url, return do_post(self.url,
json.dumps({'id': 1, 'method': method, 'params': params, json.dumps({'id': 1, 'method': method, 'params': params,
'jsonrpc': '2.0', 'Token': self.token}), 'jsonrpc': '2.0', 'Token': self.token}))
verify=False
)
class I2pdctl(object): class I2pdctl(object):
"""i2pd control""" """i2pd control"""
@ -117,9 +113,9 @@ class I2pdctl(object):
if args.json_output: if args.json_output:
print(resp.text) print(resp.text)
elif "result" in resp: elif "result" in resp:
pprint.pprint(resp.json()["result"]) pprint.pprint(resp["result"])
else: else:
pprint.pprint(resp.json()) pprint.pprint(resp)
def raw_info(self, args): def raw_info(self, args):
"""Retrieve raw JSON reply from method(s)""" """Retrieve raw JSON reply from method(s)"""
@ -130,7 +126,7 @@ class I2pdctl(object):
", ".join(INFO_METHODS.keys()))) ", ".join(INFO_METHODS.keys())))
return return
else: else:
res[m] = self.ctl.request(m, INFO_METHODS[m]).json()['result'] res[m] = self.ctl.request(m, INFO_METHODS[m])['result']
print(json.dumps(res)) print(json.dumps(res))
@ -140,9 +136,9 @@ class I2pdctl(object):
def fancy_title(string): def fancy_title(string):
print("\n### {}".format(string)) print("\n### {}".format(string))
ri_res = self.ctl.request("RouterInfo", INFO_METHODS["RouterInfo"]).json()['result'] ri_res = self.ctl.request("RouterInfo", INFO_METHODS["RouterInfo"])['result']
try: try:
csi_res = self.ctl.request("ClientServicesInfo", INFO_METHODS["ClientServicesInfo"]).json()['result'] csi_res = self.ctl.request("ClientServicesInfo", INFO_METHODS["ClientServicesInfo"])['result']
except KeyError: except KeyError:
csi_res = False csi_res = False
@ -210,7 +206,7 @@ def main():
if hasattr(args, "func"): if hasattr(args, "func"):
try: try:
args.func(args) args.func(args)
except requests.exceptions.ConnectionError: except urllib.error.URLError:
print("Error: I2PControl URL is unavailable. Check your i2pd settings and network connection.") print("Error: I2PControl URL is unavailable. Check your i2pd settings and network connection.")
exit(1) exit(1)
else: else:

Loading…
Cancel
Save