Preliminary Python3 support

This commit is contained in:
Mark Qvist 2020-04-22 12:07:13 +02:00
parent 9f8da39614
commit a339ae3d28
17 changed files with 356 additions and 259 deletions

View file

@ -61,7 +61,7 @@ def announceLoop(destination):
# destination on the network, which will let clients
# know how to create messages directed towards it.
while True:
entered = raw_input()
entered = input()
destination.announce()
RNS.log("Sent announce from "+RNS.prettyhexrep(destination.hash))
@ -86,7 +86,7 @@ def client(destination_hexhash, configpath, timeout=None):
try:
if len(destination_hexhash) != 20:
raise ValueError("Destination length is invalid, must be 20 hexadecimal characters (10 bytes)")
destination_hash = destination_hexhash.decode("hex")
destination_hash = bytes.fromhex(destination_hexhash)
except:
RNS.log("Invalid destination entered. Check your input!\n")
exit()
@ -106,7 +106,7 @@ def client(destination_hexhash, configpath, timeout=None):
# echo request to the destination specified on the
# command line.
while True:
raw_input()
input()
# Let's first check if RNS knows a path to the destination.
# If it does, we'll load the server identity and create a packet

View file

@ -62,7 +62,7 @@ def announceLoop(destination):
# destination on the network, which will let clients
# know how to create messages directed towards it.
while True:
entered = raw_input()
entered = input()
destination.announce()
RNS.log("Sent announce from "+RNS.prettyhexrep(destination.hash))
@ -116,18 +116,19 @@ def client_disconnected(link):
def client_request(message, packet):
global serve_path
if message in list_files():
filename = message.decode("utf-8")
if filename in list_files():
try:
# If we have the requested file, we'll
# read it and pack it as a resource
RNS.log("Client requested \""+message+"\"")
file = open(os.path.join(serve_path, message), "r")
RNS.log("Client requested \""+filename+"\"")
file = open(os.path.join(serve_path, filename), "rb")
file_resource = RNS.Resource(file.read(), packet.link, callback=resource_sending_concluded)
file_resource.filename = message
file_resource.filename = filename
except:
# If somethign went wrong, we close
# the link
RNS.log("Error while reading file \""+message+"\"", RNS.LOG_ERROR)
RNS.log("Error while reading file \""+filename+"\"", RNS.LOG_ERROR)
packet.link.teardown()
else:
# If we don't have it, we close the link
@ -172,7 +173,7 @@ def client(destination_hexhash, configpath):
try:
if len(destination_hexhash) != 20:
raise ValueError("Destination length is invalid, must be 20 hexadecimal characters (10 bytes)")
destination_hash = destination_hexhash.decode("hex")
destination_hash = bytes.fromhex(destination_hexhash)
except:
RNS.log("Invalid destination entered. Check your input!\n")
exit()
@ -230,11 +231,11 @@ def download(filename):
# We just create a packet containing the
# requested filename, and send it down the
# link.
request_packet = RNS.Packet(server_link, filename)
request_packet = RNS.Packet(server_link, filename.encode("utf-8"))
request_packet.send()
print("")
print("Requested \""+filename+"\" from server, waiting for download to begin...")
print(("Requested \""+filename+"\" from server, waiting for download to begin..."))
menu_mode = "download_started"
# This function runs a simple menu for the user
@ -258,7 +259,7 @@ def menu():
# Wait
time.sleep(0.25)
user_input = raw_input()
user_input = input()
if user_input == "q" or user_input == "quit" or user_input == "exit":
should_quit = True
print("")
@ -288,7 +289,7 @@ def print_menu():
print_filelist()
print("")
print("Select a file to download by entering name or number, or q to quit")
print("> "),
print(("> "), end=' ')
elif menu_mode == "download_started":
download_began = time.time()
while menu_mode == "download_started":
@ -305,12 +306,12 @@ def print_menu():
while menu_mode == "downloading":
global current_download
percent = round(current_download.progress() * 100.0, 1)
print("\rProgress: "+str(percent)+" % "),
print(("\rProgress: "+str(percent)+" % "), end=' ')
sys.stdout.flush()
time.sleep(0.1)
if menu_mode == "save_error":
print("\rProgress: 100.0 %"),
print(("\rProgress: 100.0 %"), end=' ')
sys.stdout.flush()
print("")
print("Could not write downloaded file to disk")
@ -319,16 +320,16 @@ def print_menu():
if menu_mode == "download_concluded":
if current_download.status == RNS.Resource.COMPLETE:
print("\rProgress: 100.0 %"),
print(("\rProgress: 100.0 %"), end=' ')
sys.stdout.flush()
print("")
print("The download completed! Pres enter to return to the menu.")
raw_input()
print("The download completed! Press enter to return to the menu.")
input()
else:
print("")
print("The download failed! Pres enter to return to the menu.")
raw_input()
print("The download failed! Press enter to return to the menu.")
input()
current_download = None
menu_mode = "main"
@ -431,7 +432,7 @@ def download_concluded(resource):
saved_filename = current_filename+"."+str(counter)
try:
file = open(saved_filename, "w")
file = open(saved_filename, "wb")
file.write(resource.data)
file.close()
menu_mode = "download_concluded"

81
Examples/Minimal.py Normal file
View file

@ -0,0 +1,81 @@
##########################################################
# This RNS example demonstrates a minimal setup, that #
# will start up the Reticulum Network Stack, generate a #
# new destination, and let the user send an announce. #
##########################################################
import argparse
import RNS
# Let's define an app name. We'll use this for all
# destinations we create. Since this basic example
# is part of a range of example utilities, we'll put
# them all within the app namespace "example_utilities"
APP_NAME = "example_utilitites"
# This initialisation is executed when the program is started
def program_setup(configpath):
# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)
# Randomly create a new identity for our example
identity = RNS.Identity()
# Using the identity we just created, we create a destination.
# Destinations are endpoints in Reticulum, that can be addressed
# and communicated with. Destinations can also announce their
# existence, which will let the network know they are reachable
# and autoomatically create paths to them, from anywhere else
# in the network.
destination = RNS.Destination(identity, RNS.Destination.IN, RNS.Destination.SINGLE, APP_NAME, "minimalsample")
# We configure the destination to automatically prove all
# packets adressed to it. By doing this, RNS will automatically
# generate a proof for each incoming packet and transmit it
# back to the sender of that packet. This will let anyone that
# tries to communicate with the destination know whether their
# communication was received correctly.
destination.set_proof_strategy(RNS.Destination.PROVE_ALL)
# Everything's ready!
# Let's hand over control to the announce loop
announceLoop(destination)
def announceLoop(destination):
# Let the user know that everything is ready
RNS.log("Minimal example "+RNS.prettyhexrep(destination.hash)+" running, hit enter to manually send an announce (Ctrl-C to quit)")
# We enter a loop that runs until the users exits.
# If the user hits enter, we will announce our server
# destination on the network, which will let clients
# know how to create messages directed towards it.
while True:
entered = input()
destination.announce()
RNS.log("Sent announce from "+RNS.prettyhexrep(destination.hash))
##########################################################
#### Program Startup #####################################
##########################################################
# This part of the program gets run at startup,
# and parses input from the user, and then starts
# the desired program mode.
if __name__ == "__main__":
try:
parser = argparse.ArgumentParser(description="Bare minimum example to start Reticulum and create a destination")
parser.add_argument("--config", action="store", default=None, help="path to alternative Reticulum config directory", type=str)
args = parser.parse_args()
if args.config:
configarg = args.config
else:
configarg = None
program_setup(configarg)
except KeyboardInterrupt:
print("")
exit()