Add I2PControl.py

master
TomasGl 2021-07-21 22:50:39 +03:00
commit bc70ff7a3d
No known key found for this signature in database
GPG Key ID: B35EE16AA5E15077
1 changed files with 23 additions and 0 deletions

23
I2PControl.py 100644
View File

@ -0,0 +1,23 @@
from requests import post
from json import dumps
class I2PControl:
headers = {'content-type': 'application/json'}
def __init__(self, ip):
self.host = 'https://{}:7650'.format(ip)
def auth(self, password):
data = {'id': 0, 'jsonrpc': '2.0', 'method': 'Authenticate', 'params': {'API': 2, 'Password': password}}
self.token = post(self.host, data=dumps(data), headers=self.headers, verify=False).json()['result']['Token']
def request(self, method, params):
data = {'id': 0, 'jsonrpc': '2.0', 'method': method, 'params': params | {'Token': self.token}}
# required python 3.9
return post(self.host, data=dumps(data), headers=self.headers, verify=False).json()['result']
control = I2PControl('127.0.0.1')
control.auth('itoopie')
assert control.request('Echo', {'Echo': 'echo string'})['Result'] == 'echo string'