-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclinet.py
72 lines (57 loc) · 2.03 KB
/
clinet.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
import socket
import threading
################################
# Establishing Client connection
################################
def clientNodeConnectrion(host, port, username):
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
address = (host, port)
print('Waiting for connection response')
try:
clientSocket.connect(address)
except socket.error as e:
print(str(e))
#This the first message which is sent to server to mention its username
username = sendUsername(clientSocket, username)
while True:
# #clientSocket.send(str.encode(frontClient))
# clientSocket.send(str.encode(message))
res = clientSocket.recv(1024)
print(res.decode('utf-8'))
frontClient = ''
while frontClient == '':
frontClient = input("To Whom you want send a message (Enter *all* for broadcasting): ")
Input = ''
while Input == '':
# The while loop is for inserting at least one character
Input = input(f'Hey {username}, say something: ')
message = '@' + frontClient + ':' + Input
clientSocket.send(str.encode(message))
clientSocket.close()
################################
# Sending Username to the server
################################
def sendUsername(client_S, user):
'''
This function sends a desired username to the server. If the username is taked,
the client should send another one.
'''
status = True
while status:
client_S.send(str.encode(user))
status = client_S.recv(1024)
status = status.decode('utf-8')
if status == 'True':
user = input('The username has been taken. Enter another one: ')
else:
break
return user
#*****************************************************
################################
# Main route
################################
if __name__=='__main__':
host = '127.0.0.1'
port = 2004
username = input('Enter Your name: ')
clientNodeConnectrion(host, port, username)