-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.py
107 lines (90 loc) · 3.12 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
102
103
104
105
106
"""
class Client
__init__()
@args
- serverName -> ip address default: 127.0.0.1
- serverPort -> port number default: 120000
- timer -> timer to get user input default : 10 seconds
@execution
- assign @args to local variables
- tries to connect given ip and port spesified Server
- exeption if cannot connect exits
GetUserName()
@execution
- receives a message from server
- takes input for Username
- sends username to Server
MakeQuiz()
@execution
- receives question from the server
- if it is a result stops the execution and closes the client
- calculates time before input
- Gets an input in some seconds
- Calculates the time passed for input
- Sends message to server
- Recursively calls itself until questions end
"""
from socket import *
import time
class Client:
def __init__(self,serverName="127.0.0.1",serverPort=12000,timer=10):
self.serverName = serverName
self.serverPort = serverPort
self.timer = timer
try:
self.clientSocket = socket(AF_INET, SOCK_STREAM)
self.clientSocket.connect((self.serverName, self.serverPort))
except:
print("Cannot connect to Server")
exit(1)
def GetUsername(self):
reqName = self.clientSocket.recv(1024)
print(str(reqName,'utf-8'))
name = input(" UserName : ")
self.clientSocket.send(name.encode())
def MakeQuiz(self):
comingQ = self.clientSocket.recv(1024)
print(str(comingQ, 'utf-8'))
if "Points from test:" in comingQ.decode('utf-8'):
self.clientSocket.close()
exit(0)
before = time.time()
message = input("Give answer in " + str(self.timer) +" sec: ").upper()
if time.time() - before > self.timer:
print(self.timer," seconds passed you got 0 points from this question")
message = "not possible answer given by user"
if not message:
message = "No message"
self.clientSocket.send(message.encode())
self.MakeQuiz()
if __name__ == '__main__':
client = Client()
client.GetUsername()
client.MakeQuiz()
# here is an example without class
"""
from socket import *
import time
serverName="127.0.0.1"
serverPort=12000
clientSocket=socket(AF_INET,SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
reqName = clientSocket.recv(1024)
print(reqName)
name = input("Name : ")
clientSocket.send(name.encode())
while True:
comingQ = clientSocket.recv(1024)
print(str(comingQ,'utf-8'))
if "Points from test:" in comingQ.decode('utf-8'):
clientSocket.close()
exit(0)
before = time.time()
message = input("Give answer in 10 sec: ").upper()
if time.time() - before > 10:
print("10 seconds passed you got 0 points from this question")
message = "not possible answer given by user"
if not message:
message = "No message"
clientSocket.send(message.encode())
"""