Browse Source

Reformat code with black

master
juga 1 year ago
parent
commit
e8c15e55de
2 changed files with 367 additions and 280 deletions
  1. +127
    -119
      pygnunetrest/example.py
  2. +240
    -161
      pygnunetrest/gnunet.py

+ 127
- 119
pygnunetrest/example.py View File

@ -1,186 +1,194 @@
#Examples for the Python GNUnet REST Connector
# Examples for the Python GNUnet REST Connector
import gnunet
#make instance of connector,
#without arguments it defaults to localhost, port 7776
# make instance of connector,
# without arguments it defaults to localhost, port 7776
print("\nMaking instance of connector")
conn=gnunet.GNUnetConnector()
#it is also possible to specify host and port, for example:
#conn=gnunet.GNUnetConnector("localhost","7771")
conn = gnunet.GNUnetConnector()
# it is also possible to specify host and port, for example:
# conn=gnunet.GNUnetConnector("localhost","7771")
#test if connection is possible
# test if connection is possible
print("\nTesting Connector")
if(conn.test()==True):
print("Connection established")
if conn.test() == True:
print("Connection established")
else:
print("Connection not possible")
quit()
print("Connection not possible")
quit()
#IDENTITIES#
#==========#
# IDENTITIES#
# ==========#
#first we create an identity named alice.
#the function createIdentity returns a boolean for success or failure.
# first we create an identity named alice.
# the function createIdentity returns a boolean for success or failure.
print("\nCreating some identities")
idname="alice"
success=conn.createIdentity(idname)
if(success==True):
print("Identity ",idname," created")
idname = "alice"
success = conn.createIdentity(idname)
if success == True:
print("Identity ", idname, " created")
else:
print("Error: Identity not created")
print("Error: Identity not created")
#then we create two more identities, bob and carol.
idname="bob"
success=conn.createIdentity(idname)
if(success==True):
print("Identity ",idname," created")
# then we create two more identities, bob and carol.
idname = "bob"
success = conn.createIdentity(idname)
if success == True:
print("Identity ", idname, " created")
else:
print("Error: Identity not created")
print("Error: Identity not created")
idname="carol"
success=conn.createIdentity(idname)
if(success==True):
print("Identity ",idname," created")
idname = "carol"
success = conn.createIdentity(idname)
if success == True:
print("Identity ", idname, " created")
else:
print("Error: Identity not created")
print("Error: Identity not created")
#now we retrieve a list of all existing identities.
#the function returns a list of dictionaries.
# now we retrieve a list of all existing identities.
# the function returns a list of dictionaries.
print("\nRetrieving identities")
ids=conn.getIdentityList()
ids = conn.getIdentityList()
for x in ids:
print("Name: ",x["name"],"\nPublic Key: ",x["pubkey"],"\n")
print("Name: ", x["name"], "\nPublic Key: ", x["pubkey"], "\n")
#to retrieve the public key of specific identity, we use this:
pubkey_alice=conn.getIdentityPubkey("alice")
print("Public key of alice: ",pubkey_alice)
# to retrieve the public key of specific identity, we use this:
pubkey_alice = conn.getIdentityPubkey("alice")
print("Public key of alice: ", pubkey_alice)
#to get the identity name corresponding to a public key, we use this:
name_alice=conn.getIdentityName(pubkey_alice)
print("Name for public key ",pubkey_alice, ": ",name_alice)
# to get the identity name corresponding to a public key, we use this:
name_alice = conn.getIdentityName(pubkey_alice)
print("Name for public key ", pubkey_alice, ": ", name_alice)
#next we change the name of identity "bob" to "dorothy".
# next we change the name of identity "bob" to "dorothy".
print("\Changing name of identity")
from_id="bob"
to_id="dorothy"
success=conn.changeIdentityName("bob","dorothy")
if(success==True):
print("Changed identity ",from_id," to ",to_id)
from_id = "bob"
to_id = "dorothy"
success = conn.changeIdentityName("bob", "dorothy")
if success == True:
print("Changed identity ", from_id, " to ", to_id)
else:
print("Error: Identity not changed")
print("Error: Identity not changed")
#in the end, we delete all created identities.
# in the end, we delete all created identities.
print("\nDeleting identities")
if(conn.deleteIdentity("alice")):print("deleted alice")
if(conn.deleteIdentity("dorothy")):print("deleted dorothy")
if(conn.deleteIdentity("carol")):print("deleted carol")
if conn.deleteIdentity("alice"):
print("deleted alice")
if conn.deleteIdentity("dorothy"):
print("deleted dorothy")
if conn.deleteIdentity("carol"):
print("deleted carol")
#NAMESTORE#
#=========#
# NAMESTORE#
# =========#
#to use the namestore subsystem, we first need an identity (also called 'ego')
# to use the namestore subsystem, we first need an identity (also called 'ego')
print("\nCreating identity for namestore")
idname="emily"
success=conn.createIdentity(idname)
if(success==True):
print("Identity ",idname," created")
idname = "emily"
success = conn.createIdentity(idname)
if success == True:
print("Identity ", idname, " created")
else:
print("Error: Identity not created")
print("Error: Identity not created")
#then, we have to make the created identity the default ego of namestore subsystem.
#note: the name of that identity (or ego) is also the name of the zone
# then, we have to make the created identity the default ego of namestore subsystem.
# note: the name of that identity (or ego) is also the name of the zone
print("\nMake identity the namestore default ego")
success=conn.setNamestoreIdentity(idname)
if(success==True):
print(idname," is now the namestore default identity.")
success = conn.setNamestoreIdentity(idname)
if success == True:
print(idname, " is now the namestore default identity.")
else:
print("Setting namestore default identity failed")
print("Setting namestore default identity failed")
#now we add some namestore entries to the zone.
#syntax is: addNamestoreEntry(zone,name,value,type)
# now we add some namestore entries to the zone.
# syntax is: addNamestoreEntry(zone,name,value,type)
print("\nAdding some namestore entries")
zonename=idname
success1=conn.addNamestoreEntry(zonename,"google","8.8.8.8","A")
success2=conn.addNamestoreEntry(zonename,"pepfoundation","95.128.36.146","A")
success3=conn.addNamestoreEntry(zonename,"gnunet","131.159.74.67","A")
if(success1 and success2 and success3):
print("3 records created")
zonename = idname
success1 = conn.addNamestoreEntry(zonename, "google", "8.8.8.8", "A")
success2 = conn.addNamestoreEntry(
zonename, "pepfoundation", "95.128.36.146", "A"
)
success3 = conn.addNamestoreEntry(zonename, "gnunet", "131.159.74.67", "A")
if success1 and success2 and success3:
print("3 records created")
else:
print("failure creating records")
print("failure creating records")
#we can change existing namestore entries.
# we can change existing namestore entries.
print("\nChanging namestore entry")
success=conn.changeNamestoreEntry(zonename,"google","6.6.6.6","A")
if(success):
print("Namestore entry changed")
success = conn.changeNamestoreEntry(zonename, "google", "6.6.6.6", "A")
if success:
print("Namestore entry changed")
else:
print("failure changing namestore entry")
print("failure changing namestore entry")
#now we show all existing namestore entries.
# now we show all existing namestore entries.
print("\nRetrieving namestore entries")
entries=conn.getNamestoreEntries(zonename)
entries = conn.getNamestoreEntries(zonename)
for x in entries:
print("\n")
print("Name: ",x["record_name"])
for y in x["data"]:
print("Value: ",y["value"]," Type:",y["record_type"])
print("\n")
print("Name: ", x["record_name"])
for y in x["data"]:
print("Value: ", y["value"], " Type:", y["record_type"])
#delete all the namestore entries
# delete all the namestore entries
print("\nDeleting all namestore entries")
success1=conn.deleteNamestoreEntry(zonename,"google")
success2=conn.deleteNamestoreEntry(zonename,"pepfoundation")
success3=conn.deleteNamestoreEntry(zonename,"gnunet")
if(success1 and success2 and success2):
print("Namestore entries successfully deleted")
success1 = conn.deleteNamestoreEntry(zonename, "google")
success2 = conn.deleteNamestoreEntry(zonename, "pepfoundation")
success3 = conn.deleteNamestoreEntry(zonename, "gnunet")
if success1 and success2 and success2:
print("Namestore entries successfully deleted")
else:
print("Error deleting namestore entries")
print("Error deleting namestore entries")
#GNS#
#===#
# GNS#
# ===#
#first we create a test record with 3 namestore entries.
# first we create a test record with 3 namestore entries.
print("\nCreating namestore entries")
zonename=idname
success1=conn.addNamestoreEntry(zonename,"testrecord","95.128.36.146","A")
success2=conn.addNamestoreEntry(zonename,"testrecord","Notes for the record","TXT")
success3=conn.addNamestoreEntry(zonename,"testrecord","Further notes for the record","TXT")
#success3=True
#forgot why i did this
if(success1 and success2 and success3):
print("3 records created")
zonename = idname
success1 = conn.addNamestoreEntry(zonename, "testrecord", "95.128.36.146", "A")
success2 = conn.addNamestoreEntry(
zonename, "testrecord", "Notes for the record", "TXT"
)
success3 = conn.addNamestoreEntry(
zonename, "testrecord", "Further notes for the record", "TXT"
)
# success3=True
# forgot why i did this
if success1 and success2 and success3:
print("3 records created")
else:
print("failure creating records")
print("failure creating records")
#then we retrieve all entries for the test record
# then we retrieve all entries for the test record
print("\nRetrieving GNS records by name")
entries=conn.getGNSValuesOfName(zonename,"testrecord")
entries = conn.getGNSValuesOfName(zonename, "testrecord")
print("\n")
print("Record name: ",entries["record_name"])
print("Record name: ", entries["record_name"])
for x in entries["data"]:
print("Type: ",x["record_type"]," Value: ",x["value"])
print("Type: ", x["record_type"], " Value: ", x["value"])
#now we only retrieve entries for the test of type "TXT"
# now we only retrieve entries for the test of type "TXT"
print("\nRetrieving GNS records by name and type")
entries=conn.getGNSValuesOfNameAndType(zonename,"testrecord","TXT")
entries = conn.getGNSValuesOfNameAndType(zonename, "testrecord", "TXT")
print("\n")
print("Record name: ",entries["record_name"])
print("Record name: ", entries["record_name"])
for x in entries["data"]:
print("Type: ",x["record_type"]," Value: ",x["value"])
print("Type: ", x["record_type"], " Value: ", x["value"])
#in the end, we delete the test record.
# in the end, we delete the test record.
print("\nDeleting test record")
success=conn.deleteNamestoreEntry(zonename,"testrecord")
if(success):
print("Namestore entry successfully deleted")
success = conn.deleteNamestoreEntry(zonename, "testrecord")
if success:
print("Namestore entry successfully deleted")
else:
print("Error deleting namestore entries")
print("Error deleting namestore entries")

+ 240
- 161
pygnunetrest/gnunet.py View File

@ -2,165 +2,244 @@ import requests
import json
import socket
class GNUnetConnector:
def __init__(self,host="localhost",port="7776"):
self.host=host
self.port=port
def test(self):
#check if port is open
#not the best solution, it should better check if REST responds \
#and not only if port is listening
testsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
testlocation = (self.host, int(self.port))
testresult = testsocket.connect_ex(testlocation)
testsocket.close()
if testresult == 0:
return True
else:
return False
# Identity Section
def getIdentityList(self):
#returns a list of all identities
requeststring="http://"+self.host+":" \
+self.port+"/identity/"
respdata=requests.get(requeststring).json()
val=json.loads(json.dumps(respdata))
return(val)
def getIdentityPubkey(self,thename):
#returns the public key of an identity specified by name
requeststring="http://"+self.host+":" \
+self.port+"/identity/name/"+thename
respdata=requests.get(requeststring).json()
val=json.loads(json.dumps(respdata))
return(val["pubkey"])
def getIdentityName(self,thepubkey):
#returns the name of an identity specified by public key
requeststring="http://"+self.host+":" \
+self.port+"/identity/pubkey/"+thepubkey
respdata=requests.get(requeststring).json()
val=json.loads(json.dumps(respdata))
return(val["name"])
def getNamestoreIdentity(self):
#returns the default identity of subsystem 'namestore'
requeststring="http://"+self.host+":" \
+self.port+"/identity/subsystem/namestore"
respdata=requests.get(requeststring).json()
val=json.loads(json.dumps(respdata))
return(val["name"])
def createIdentity(self,thename):
#creates Identy, returns True if successful, False if not
requeststring="http://"+self.host+":" \
+self.port+"/identity"
datadict={"name": thename}
dataparams=json.dumps(datadict)
respdata=requests.post(requeststring,data=dataparams)
if("201" in str(respdata)):
#successful
return True
else:
#not successful
return False
return()
def changeIdentityName(self,thename,newname):
#changes name of identity, returns True if successful, False if not
requeststring="http://"+self.host+":" \
+self.port+"/identity/name/"+thename
datadict={"newname": newname}
dataparams=json.dumps(datadict)
respdata=requests.put(requeststring,data=dataparams)
if("204" in str(respdata)):
#successful
return True
else:
return False
def deleteIdentity(self,thename):
#deletes Identity, returns True if successful, false if not
requeststring="http://"+self.host+":" \
+self.port+"/identity/name/"+thename
respdata=requests.delete(requeststring)
if("204" in str(respdata)):
#successful
return True
else:
return False
# Namestore Section
def setNamestoreIdentity(self,thename):
#sets the default Identity for subsystem 'namestore'
#returns True if successful False if not
requeststring="http://"+self.host+":" \
+self.port+"/identity/subsystem/"+thename
datadict={"subsystem": "namestore"}
dataparams=json.dumps(datadict)
respdata=requests.put(requeststring,data=dataparams)
if("204" in str(respdata)):
return True
else:
return False
def getNamestoreEntries(self,thezone):
#returns a list of all namestore entries
requeststring="http://"+self.host+":" \
+self.port+"/namestore/"+thezone
respdata=requests.get(requeststring).json()
val=json.loads(json.dumps(respdata))
return(val)
#return(respdata)
def addNamestoreEntry(self,thezone,thename,thevalue,thetype):
#adds a namestore entry, returns true if successful, false if not
requeststring="http://"+self.host+":" \
+self.port+"/namestore/"+thezone
dataparams="{\"data\": [{\"value\": \""+thevalue+"\", \"record_type\": \""+thetype+"\", \"expiration_time\":\"1d\", \"private\": false, \"relative_expiration\": false, \"supplemental\": false, \"shadow\": false}], \"record_name\": \""+thename+"\"}"
respdata=requests.post(requeststring,data=dataparams)
if("204" in str(respdata)):
return True
else:
return False
def changeNamestoreEntry(self,thezone,thename,thevalue,thetype):
#changes a namestore entry, returns true if successful, false if not
requeststring="http://"+self.host+":" \
+self.port+"/namestore/"+thezone
dataparams="{\"data\": [{\"value\": \""+thevalue+"\", \"record_type\": \""+thetype+"\", \"expiration_time\":\"1d\", \"private\": false, \"relative_expiration\": false, \"supplemental\": false, \"shadow\": false}], \"record_name\": \""+thename+"\"}"
respdata=requests.put(requeststring,data=dataparams)
if("204" in str(respdata)):
return True
else:
return False
def deleteNamestoreEntry(self,thezone,thename):
#deletes a namestore entry, returns true if successful, false if not
requeststring="http://"+self.host+":" \
+self.port+"/namestore/"+thezone+"/"+thename
respdata=requests.delete(requeststring)
if("204" in str(respdata)):
return True
else:
return False
#GNS section
def getGNSValuesOfName(self,thezone,thename):
#returns a list of values and record types with the same record name
requeststring="http://"+self.host+":" \
+self.port+"/gns/"+thename+"."+thezone
respdata=requests.get(requeststring).json()
val=json.loads(json.dumps(respdata))
return(val)
def getGNSValuesOfNameAndType(self,thezone,thename,thetype):
#returns a list of values of a certain name and record type
requeststring="http://"+self.host+":" \
+self.port+"/gns/"+thename+"."+thezone+"?record_type="+thetype
respdata=requests.get(requeststring).json()
val=json.loads(json.dumps(respdata))
return(val)
def __init__(self, host="localhost", port="7776"):
self.host = host
self.port = port
def test(self):
# check if port is open
# not the best solution, it should better check if REST responds \
# and not only if port is listening
testsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
testlocation = (self.host, int(self.port))
testresult = testsocket.connect_ex(testlocation)
testsocket.close()
if testresult == 0:
return True
else:
return False
# Identity Section
def getIdentityList(self):
# returns a list of all identities
requeststring = "http://" + self.host + ":" + self.port + "/identity/"
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val
def getIdentityPubkey(self, thename):
# returns the public key of an identity specified by name
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/identity/name/"
+ thename
)
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val["pubkey"]
def getIdentityName(self, thepubkey):
# returns the name of an identity specified by public key
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/identity/pubkey/"
+ thepubkey
)
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val["name"]
def getNamestoreIdentity(self):
# returns the default identity of subsystem 'namestore'
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/identity/subsystem/namestore"
)
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val["name"]
def createIdentity(self, thename):
# creates Identy, returns True if successful, False if not
requeststring = "http://" + self.host + ":" + self.port + "/identity"
datadict = {"name": thename}
dataparams = json.dumps(datadict)
respdata = requests.post(requeststring, data=dataparams)
if "201" in str(respdata):
# successful
return True
else:
# not successful
return False
return ()
def changeIdentityName(self, thename, newname):
# changes name of identity, returns True if successful, False if not
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/identity/name/"
+ thename
)
datadict = {"newname": newname}
dataparams = json.dumps(datadict)
respdata = requests.put(requeststring, data=dataparams)
if "204" in str(respdata):
# successful
return True
else:
return False
def deleteIdentity(self, thename):
# deletes Identity, returns True if successful, false if not
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/identity/name/"
+ thename
)
respdata = requests.delete(requeststring)
if "204" in str(respdata):
# successful
return True
else:
return False
# Namestore Section
def setNamestoreIdentity(self, thename):
# sets the default Identity for subsystem 'namestore'
# returns True if successful False if not
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/identity/subsystem/"
+ thename
)
datadict = {"subsystem": "namestore"}
dataparams = json.dumps(datadict)
respdata = requests.put(requeststring, data=dataparams)
if "204" in str(respdata):
return True
else:
return False
def getNamestoreEntries(self, thezone):
# returns a list of all namestore entries
requeststring = (
"http://" + self.host + ":" + self.port + "/namestore/" + thezone
)
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val
# return(respdata)
def addNamestoreEntry(self, thezone, thename, thevalue, thetype):
# adds a namestore entry, returns true if successful, false if not
requeststring = (
"http://" + self.host + ":" + self.port + "/namestore/" + thezone
)
dataparams = (
'{"data": [{"value": "'
+ thevalue
+ '", "record_type": "'
+ thetype
+ '", "expiration_time":"1d", "private": false, "relative_expiration": false, "supplemental": false, "shadow": false}], "record_name": "'
+ thename
+ '"}'
)
respdata = requests.post(requeststring, data=dataparams)
if "204" in str(respdata):
return True
else:
return False
def changeNamestoreEntry(self, thezone, thename, thevalue, thetype):
# changes a namestore entry, returns true if successful, false if not
requeststring = (
"http://" + self.host + ":" + self.port + "/namestore/" + thezone
)
dataparams = (
'{"data": [{"value": "'
+ thevalue
+ '", "record_type": "'
+ thetype
+ '", "expiration_time":"1d", "private": false, "relative_expiration": false, "supplemental": false, "shadow": false}], "record_name": "'
+ thename
+ '"}'
)
respdata = requests.put(requeststring, data=dataparams)
if "204" in str(respdata):
return True
else:
return False
def deleteNamestoreEntry(self, thezone, thename):
# deletes a namestore entry, returns true if successful, false if not
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/namestore/"
+ thezone
+ "/"
+ thename
)
respdata = requests.delete(requeststring)
if "204" in str(respdata):
return True
else:
return False
# GNS section
def getGNSValuesOfName(self, thezone, thename):
# returns a list of values and record types with the same record name
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/gns/"
+ thename
+ "."
+ thezone
)
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val
def getGNSValuesOfNameAndType(self, thezone, thename, thetype):
# returns a list of values of a certain name and record type
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/gns/"
+ thename
+ "."
+ thezone
+ "?record_type="
+ thetype
)
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val

Loading…
Cancel
Save