-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.py
101 lines (75 loc) · 2.6 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- coding: utf-8 -*-
import socket
import json
from MessageReceiver import MessageReceiver
from MessageParser import MessageParser
class Client:
"""
This is the chat client class
"""
def __init__(self, host, server_port):
"""
This method is run when creating a new Client object
"""
# Set up the socket connection to the server
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
adress = ('localhost', 9998)
self.connection.connect(adress)
receiver = MessageReceiver(self, self.connection)
receiver.start()
self.running = True
self.run()
def run(self):
# Initiate the connection to the server
while self.running:
Client.interface(self)
def disconnect(self):
# TODO: Handle disconnection
self.connection.close()
self.running = False
print("Du er nå logget av")
def receive_message(self, message):
print 'in receive'
MessageParser.parse(message)
print 'after parse'
def send_payload(self, data):
self.connection.send(data)
pass
def msg(self, brukerinput):
msg = Client.jsonconv(self, brukerinput, "msg")
self.send_payload(msg)
def send_help(self):
self.send_payload(self.jsonconv('help',None ))
def login(self, login, username):
self.send_payload((self.jsonconv(login, username)))
def name(self):
self.send_payload(self.jsonconv(('name', None)))
def jsonconv(self, request, content):
temp = {'request': request, 'content': content}
output = json.dumps({'request': request, 'content': content}, indent=4, separators=(',', ': '))
return output
def interface(self):
brukerinput=raw_input("Hva vil du gjøre?")
if brukerinput == "help":
self.send_help()
elif brukerinput == "name":
pass
elif brukerinput == "login":
brukernavn = raw_input("Ditt brukernavn?")
self.login(brukerinput, brukernavn)
elif brukerinput == "msg":
beskjed = raw_input("Hva er din beskjed?")
self.msg(brukerinput, beskjed)
elif brukerinput == "logout":
self.disconnect()
output = None
else:
print("Dette er ikke en lovlig kommando")
output = None
if __name__ == '__main__':
"""
This is the main method and is executed when you type "python Client.py"
in your terminal.
No alterations are necessary
"""
client = Client('localhost', 9998)