def main(): username = input("Please enter your username: "******"zerochat_" + username) oldchats = {} while True: chats = {name:address for name, address in nw0.discover_all() if name.startswith("zerochat_")} if chats.keys() != oldchats.keys(): print("New users:") print(chats) rlist, wlist, elist = select.select([sys.stdin], [], [], 0.1) if rlist: recipient = sys.stdin.readline().rstrip() msg = input("Message:").rstrip() print("\x1b[31;m[%s] %s\x1b[0m" % (recipient, msg)) recipient_service = nw0.discover("zerochat_" + recipient) nw0.send_message_to(recipient_service, msg) received_msg = nw0.wait_for_message_from(address, wait_for_s=0, autoreply=True) if received_msg is not None: print("Got message: %s" % received_msg) oldchats = chats
def play_first(choose_move): moves = [] # discover my_address = nw0.discover('RPS') # we don't know their move yet! my_move = decide_move(choose_move, their_move='None') my_salt = make_salt() my_hash = make_hash(my_move, my_salt) print(my_salt, my_hash) # comm 1 # send my hash # recv their hash back their_hash = nw0.send_message_to(my_address, my_hash) print('I send a sealed {}'.format(my_move)) # comm 2 # send my move + salt # recv their move + salt back their_move_char, their_salt = nw0.send_message_to( my_address, (my_move.char, my_salt)) their_move = RPS(their_move_char) check_hash(their_move, their_salt, their_hash) moves.append(my_move) moves.append(their_move) print('I play', my_move) print('They play', their_move) return decide_winner(moves)
def main(name=None): name = name or input("Name: ") nw0.send_message_to(hub, ["JOIN", name]) try: while True: try: message = input("Message: ") except KeyboardInterrupt: message = None if not message: break nw0.send_message_to(hub, ["SPEAK", (name, message)]) finally: nw0.send_message_to(hub, ["LEAVE", name])
def play_first(choose_move): moves = [] # discover my_address = nw0.discover('RPS') if isinstance(choose_move, six.string_types): my_move = RPS(choose_move) else: # we don't know their move yet! my_move = choose_move(their_move=None) # send my move, and get their move back their_move = RPS( nw0.send_message_to(my_address, my_move.char) ) moves.append(my_move) moves.append(their_move) print('I play', my_move) print('They play', their_move) return decide_winner(moves)
# # Wait 30 seconds to discover all neighbours # print("Waiting %d seconds for neighbours to show up..." % N_SECONDS_WAIT_FOR_NEIGHBOURS) time.sleep(N_SECONDS_WAIT_FOR_NEIGHBOURS) print("Looking for neighbours") addresses = [ address for (name, address) in nw0.discover_group("halifax", exclude=[my_name]) ] print(addresses) while True: if first_word: word = first_word first_word = None else: print("Waiting for next word...") word = nw0.wait_for_message_from(my_address, autoreply=True) print("Got word", word) candidate_words = words[word[-1]] random.shuffle(candidate_words) next_word = candidate_words.pop() print("Sending word", next_word) nw0.send_message_to(random.choice(addresses), next_word)
import networkzero as nwz quiz_server = nwz.discover("Quiz") reply = nwz.send_message_to(quiz_server, "World") question = reply.split('|') print(question[0]) for i in range(1, len(question)): letter = chr(64 + i) print(letter + ") " + question[i]) print("Enter the letter of your answer") answer = input(">") response = ord(answer) - 64 message = nwz.send_message_to(quiz_server, question[response]) print(message)
else: first_word = '' N_SECONDS_WAIT_FOR_NEIGHBOURS = 5 # # Wait 30 seconds to discover all neighbours # print("Waiting %d seconds for neighbours to show up..." % N_SECONDS_WAIT_FOR_NEIGHBOURS) time.sleep(N_SECONDS_WAIT_FOR_NEIGHBOURS) print("Looking for neighbours") addresses = [address for (name, address) in nw0.discover_group("halifax", exclude=[my_name])] print(addresses) while True: if first_word: word = first_word first_word = None else: print("Waiting for next word...") word = nw0.wait_for_message_from(my_address, autoreply=True) print("Got word", word) candidate_words = words[word[-1]] random.shuffle(candidate_words) next_word = candidate_words.pop() print("Sending word", next_word) nw0.send_message_to(random.choice(addresses), next_word)
def test_send_to_multiple_addresses(support): address1 = nw0.core.address() address2 = nw0.core.address() message = uuid.uuid4().hex with pytest.raises(nw0.core.InvalidAddressError): nw0.send_message_to([address1, address2], message)
def get_key(key): return networkzero.send_message_to(server, json.dumps({ "cmd": "GET", "key": key }))
import ast import itertools import sys import time import networkzero as nw0 t = 'server_team_4' a = nw0.discover(t) def get_ans(question, n): return input("What's the answer? ") if len(sys.argv) > 1 and sys.argv[1] == 'hard': from t1000 import get_ans for round in itertools.count(): # print('ROUND {}'.format(round)) question = nw0.send_message_to(a, "-> I'm ready!") print('<- {}'.format(question)) ans = get_ans(question, round) print("-> {}".format(ans)) n = nw0.send_message_to(a, ans) print('<- {}'.format(n)) print()
def test_send_message_with_timeout(support): address = nw0.core.address() with pytest.raises(nw0.core.SocketTimedOutError): nw0.send_message_to(address, wait_for_reply_s=1.0)
player_ID = 0 # Search for messages from participants until roster is full while len(players) < MAX_PLAYERS: # Poll the server checking for rquests from participants message = nwz.wait_for_message_from(quiz_server, wait_for_s=0) # If a message has been recieved if message is not None: # Capture the address for the connection to the quiz client address = message print(address) conn = nwz.discover(address) players.append(Player(player_ID, address, conn)) player_ID += 1 nwz.send_reply_to(quiz_server, "Player acknowledged") reply = nwz.send_message_to(conn, "Welcome to the quiz") message = None else: sleep(1) if len(players) > 0: reply = nwz.send_message_to( players[0].connection, "Waiting for... " + str(len(players) - MAX_PLAYERS)) questions = [] with open("../questions.csv", 'r') as file: for line in file.readlines(): line = line.split(',') if len(line) > 4: new_q = Question(line[0], line[1:5], line[5]) questions.append(new_q)
import networkzero as nw0 service = nw0.discover("messenger1") reply = nw0.send_message_to(service, "This is a message") print("Reply: ", reply)
import os, sys import networkzero as nw0 try: input = raw_input except NameError: pass gallery = nw0.discover("gallery") filepath = input("Filename: ") filename = os.path.basename(filepath) with open(filepath, "rb") as f: data = f.read() nw0.send_message_to(gallery, (filename, nw0.bytes_to_string(data)))
import networkzero as nw0 address = nw0.advertise("service") message = nw0.wait_for_message_from(address) print("Message received: ", message) nw0.send_message_to(address, "Received: %s" % message)
text += str(dead) text += " (" text += str(round(percent(dead, Constants.POPULATION), 2)) text += "%)" if not do: text += " [PAUSED]" print("\b"*(len(text) + 19), end="") print(text, end="") counter += 1 if counter > (Constants.SPEED / 2) - 1: counter = 0 try: nw0.send_message_to(console, {"generation": str(generation), "iteration": str(instruction), "bestlastgen": str(round(best_last_round, 2)), "bestlastgenper": str(round(percent(best_last_round, length), 2)), "bestthisgen": str(round(best_score, 2)), "bestthisgenper": str(round(percent(best_score, length), 2)), "dnamutcount": str(mutations), "dead": str(dead), "deadper": str(round(percent(dead, Constants.POPULATION), 2)), "paused": not do}) except NameError: pass pygame.display.flip() clock.tick(Constants.SPEED) pygame.quit()
from __future__ import print_function import networkzero as nw0 try: # Python 2.7 compat input = raw_input except NameError: pass service = nw0.discover("anagram") while True: anagram = input("Enter anagram: ") word = nw0.send_message_to(service, anagram) if word: print(word) else: print("Nothing found")
name = "cluster/master" address = nw0.advertise(name) # # On the cluster master # nodes = set(nw0.discover_group("cluster", exclude=name)) old_nodes = nodes print("Nodes:", ", ".join(nodes)) # # Send a command to A .... which will unaccountably fail # node_a = nw0.discover("cluster/A") nw0.send_message_to(node_a, "STOP") # # Wait a few seconds for node C to wake up # time.sleep(5) # # On the cluster master # nodes = set(nw0.discover_group("cluster")) for name, address in old_nodes - nodes: print("%s has left the cluster" % name) for name, address in nodes - old_nodes: print("%s has joined the cluster" % name)
import networkzero as nwz from time import sleep quiz_server = nwz.discover("Quiz") address = nwz.address() connection = nwz.advertise(address) reply = nwz.send_message_to(quiz_server, address) print(reply) quiz_starting = False while not quiz_starting: reply = nwz.wait_for_message_from(quiz_server, wait_for_s=0, autoreply=True) if reply is not None: print(reply) else: reply = nwz.wait_for_message_from(connection, wait_for_s=0, autoreply=True) if reply is not None: print(reply) if reply == "Quiz Starting": quiz_starting = True sleep(1) print("Quiz loop starting") quiz_over = False while not quiz_over: reply = nwz.wait_for_message_from(connection, wait_for_s=0, autoreply=True) if reply is not None: #print(reply) if reply == "Next Question":
import networkzero as nwz question = "What colour is the sky?|Red|Purple|Blue|Yellow" answer = "Blue" quiz_server = nwz.advertise('Quiz') while True: nwz.wait_for_message_from(quiz_server) reply = nwz.send_message_to(quiz_server, question) print("Answer was... " + reply) if answer == reply: correct = "Correct" else: correct = "Wrong Answer" nwz.send_message_to(quiz_server, correct)
player_ID = 0 # Search for messages from participants until roster is full while len(players) < MAX_PLAYERS: # Poll the server checking for requests from participants connections = nwz.discover_all() #print(connections) # If a message has been recieved if len(connections) > 0: for c in connections: # Capture the address for the connection to the quiz client if "Quiz Participant" in c[0] and c[1] not in player_addresses: print(connections) nwz.send_news_to(quiz_server, "Information", "Player acknowledged" + c[1]) conn = nwz.discover(c[0]) response = nwz.send_message_to(conn, player_ID) nwz.send_reply_to(conn) players.append(Player(player_ID, c[1], conn)) player_addresses.append(c[1]) player_ID += 1 else: sleep(1) if len(players) > 0: nwz.send_news_to(quiz_server, "Information", "Waiting for... " + str(MAX_PLAYERS -len(players)) + " players") sleep (5) questions = [] with open("questions.csv", 'r') as file: for line in file.readlines(): line = line.split(',') if len(line) > 4:
import networkzero as nw0 try: # Python 2.7 compat input = raw_input except NameError: pass reverso = nw0.discover("reverso") while True: word = input("Enter word: ") reversed_word = nw0.send_message_to(reverso, word) print("Reversed:", reversed_word)
def test_send_message(support): address = nw0.core.address() message = uuid.uuid4().hex support.queue.put(("send_message_to", [address])) reply = nw0.send_message_to(address, message) assert reply == message
def test_send_message_empty(support): address = nw0.core.address() support.queue.put(("send_message_to", [address])) reply = nw0.send_message_to(address) assert reply == nw0.messenger.EMPTY
def set_key(key, value): return networkzero.send_message_to(server, json.dumps({ "cmd": "SET", "key": key, "data": value }))
import networkzero as nw0 service = nw0.discover("messenger2") nw0.send_message_to(service, "This is a command")
import networkzero as nwz from time import sleep quiz_server = nwz.discover("Quiz") address = nwz.address() connection = nwz.advertise("Quiz Participant :s" + address) response = nwz.wait_for_message_from(connection, autoreply=True) playerID = response response = nwz.wait_for_message_from(connection) response = "" while response != "Done": news = nwz.wait_for_news_from(quiz_server) if news[0] == "Information": print("Quiz server says: " + news[1]) elif news[0] == "Question": print("Incoming Question") print(news[1]) elif news[0] == "Answers": answers = news[1] for i in range(len(answers)): print(str(i) + ". " + answers[i]) print('Enter the number of your answer') my_answer = int(input(">")) reply = nwz.send_message_to(connection, [playerID, my_answer]) print(reply) elif news[0] == playerID: print(news[1])