-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
102 lines (80 loc) · 2.69 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
import socket
class Client:
"""
Create a client socket that link to a certain port, and communicate with the server.
The aim of this Client is to connect to a server through a port.
After correctly connected, you can send messages/data to the server using the
chat_with_server function, that can send several messages.
To stop the client, send the message "fin"
"""
def __init__(self, port, host="localhost"):
self.host = host
self.port = int(port)
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self):
"""
Trying to connect to self.host : self.port
"""
try:
self.socket.connect((self.host, self.port))
#self.socket.setblocking(0)
self.socket.settimeout(2)
print("Connection on {}".format(self.port))
except:
print("Connection to {} failed".format(self.host))
def send_message(self, message):
"""
Send a message to the connected server
"""
self.socket.send(message.encode())
response = ""
while not response:
response = self.socket.recv(1024)
print(response.decode())
return response.decode()
# print("Close")
# self.socket.close()
def test_connection(self):
response = self.send_message("TEST_CO")
if response == "1":
return True
else:
return False
def chat_with_server(self):
"""
Chat with server by allow to send several messages
"""
try:
self.socket.send('test'.encode())
except:
print("No connection to a server.")
return 0
mess = input("Which message to send to the server : \n")
server_response = ""
while mess != "fin":
self.socket.send(mess.encode())
try:
server_response = self.socket.recv(1024)
except socket.timeout:
pass
print(server_response)
mess = input(">>> ")
self.socket.send(mess.encode())
server_response = self.socket.recv(1024)
print(server_response.decode())
def close(self):
print("Close")
self.socket.close()
if __name__ == '__main__':
hote = "localhost"
port = 15555
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect((hote, port))
print("Connection on {}".format(port))
response = ""
while not response:
socket.send('Hello world'.encode())
response = socket.recv(1024)
print("<"+response.decode())
print("Close connexion")
socket.close()