-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
78 lines (67 loc) · 2.21 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
import socket
import sys
import select
import errno
import pdb
class Chatclient:
BUFFERSIZE = 1024
def __init__(self):
self.input_from = []
self.ns_socket = None
self.input_from.append(sys.stdin)
def connect_to_ns(self, ns_ip, ns_port):
"""
Establish a connection to the name server and
preform the required handshake
"""
try:
self.ns_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.ns_socket.connect((ns_ip, int(ns_port)))
self.ns_socket.send("HELLO")
msg = self.ns_socket.recv(self.BUFFERSIZE)
if msg != '100 CONNECTED':
self.ns_socket = None
print msg
except Exception as e:
self.ns_socket = None
print "failed to connect to the given address"
print e
def disconnect_from_server(self):
"""
Close the connection properly to the name server
"""
self.ns_socket.send('LEAVE')
response = self.ns_socket.recv(self.BUFFERSIZE)
if response.split()[0] == '400':
self.ns_socket.close()
self.ns_socket = None
print "Successfully disconnected from name server"
def request_post(self, name, time):
"""
Request the connected highscore server to post a time on it's scoreboard
"""
self.ns_socket.send('POST %s %d' % (name, time))
print self.ns_socket.recv(self.BUFFERSIZE)
def request_top(self, n=10):
"""
Get the list of online users and print it using nice formating
"""
#pdb.set_trace()
self.ns_socket.send('TOP %d' % n)
response = self.ns_socket.recv(self.BUFFERSIZE)
tokens = response.split()
if tokens[0] == '300':
print 'Top %s:' % tokens[2]
for i in xrange(3, len(tokens)-1, 2):
print "%s: %s" % (tokens[i], tokens[i+1])
else:
print "Something went wrong"
print response
# Run the client.
if __name__ == "__main__":
client = Chatclient()
print 1
client.connect_to_ns('localhost', 6789)
print 2
client.request_top()