Created
September 9, 2016 23:27
-
-
Save mskeving/6094a5ec0603eb270280e3cb761f232c to your computer and use it in GitHub Desktop.
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 sys | |
from collections import defaultdict | |
import comm | |
import comm_nonblock | |
def main(): | |
args = sys.argv[1:] | |
if len(args) < 1: | |
sys.stderr.write("Expecting one or more arguments, got None.") | |
sys.exit(1) | |
page_names = [arg for arg in args] | |
all_sockets = [] | |
sockid_to_req = {} | |
for page_name in page_names: | |
sock = comm_nonblock.connect(comm.PASSWORD_SERVER) | |
sock_id = sock.getsockname()[1] | |
req = json.dumps(dict(page=page_name)) | |
sockid_to_req[sock_id] = req | |
all_sockets.append(sock) | |
# send all requests | |
finished_sockets = 0 | |
while finished_sockets < len(all_sockets): | |
for sock in all_sockets: | |
sock_id = sock.getsockname()[1] | |
req = sockid_to_req[sock_id] | |
if req is None: | |
continue | |
new_req = comm_nonblock.send(sock, req) | |
if new_req is None: | |
finished_sockets += 1 | |
sockid_to_req[sock_id] = new_req | |
# read all requests | |
sockid_to_resp = defaultdict(list) | |
finished_sockets = [] | |
while len(finished_sockets) < len(all_sockets): | |
for sock in all_sockets: | |
sock_id = sock.getsockname()[1] | |
if sock_id in finished_sockets: | |
continue | |
data_part = comm_nonblock.recv(sock) | |
if data_part is None: | |
finished_sockets.append(sock_id) | |
print_password(sockid_to_resp[sock_id]) | |
elif len(data_part) > 0: | |
sockid_to_resp[sock_id].append(data_part) | |
def print_password(resp): | |
data = b''.join(resp) | |
resp = json.loads(data) | |
if 'ok' not in resp: | |
raise Exception("link server said: {!r}".format(resp)) | |
print "password: ", resp['ok'] | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment