-
-
Save NESS-Network/66d5cbca7d156d04ebc46856bbf4b9f4 to your computer and use it in GitHub Desktop.
Emercoin json-rpc simple python driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
class EmercoinClient(object): | |
def __init__(self, | |
user='rpcuser from emercoin.conf', | |
password='rpcpassword from emercoin.conf', | |
protocol='http', | |
host='hostname or IP where EMC is running', | |
port='port, that emercoin is listening'): | |
self.allowed_methods = ['name_new', 'name_show', 'name_mempool', 'name_update', 'name_list'] | |
self.user = user | |
self.password = password | |
self.protocol = protocol | |
self.host = host | |
self.port = port | |
def call_command(self, command, *args): | |
credentials = f"{self.user}:{self.password}@" | |
url = f"{self.protocol}://{credentials}{self.host}:{self.port}" | |
data = { 'method': command } | |
if args: | |
data['params'] = args | |
try: | |
r = requests.post(url=url, data=json.dumps(data)) | |
data = r.json() | |
except requests.ConnectionError: | |
return {'result': None, 'error':{'code': 500, 'message': "can't connect server"}} | |
except ValueError: | |
error_msg = "can't connect server" | |
if r.status_code == 401: | |
error_msg = 'unauthorized' | |
return {'result': None, 'error':{'code': r.status_code, 'message': error_msg}} | |
return data | |
def name_new(self): | |
name = input("name ") | |
value = input("value ") | |
days = int(input("days ")) | |
return self.call_command('name_new', name, value, days) | |
def name_update(self): | |
name = input("name ") | |
value = input("value ") | |
days = int(input("days ")) | |
return self.call_command('name_update', name, value, days) | |
def name_show(self): | |
name = input("name ") | |
return self.call_command('name_show', name) | |
def name_list(self): | |
return self.call_command('name_list') | |
def name_mempool(self): | |
return self.call_command('name_mempool') | |
if __name__ == '__main__': | |
# | |
client = EmercoinClient(user='emccoinrpc', password='123456', host='cantdoevil.com', | |
port=6662, protocol='http') | |
print("Hi! I'm a simple emercoin client. Keep attention for what you're writing") | |
while True: | |
print("You can use this methods:") | |
print(*client.allowed_methods) | |
print() | |
command = input('input method name ') | |
if command not in client.allowed_methods: | |
print('please use allowed methods') | |
print(*client.allowed_methods) | |
print() | |
elif command == 'name_new': | |
result = client.name_new() | |
print(result) | |
elif command == 'name_update': | |
result = client.name_update() | |
print(result) | |
elif command == 'name_mempool': | |
result = client.name_mempool() | |
print(result) | |
elif command == 'name_list': | |
result = client.name_list() | |
print(result) | |
elif command == 'name_show': | |
result = client.name_show() | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment