-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient Code.py
More file actions
49 lines (36 loc) · 1.3 KB
/
Client Code.py
File metadata and controls
49 lines (36 loc) · 1.3 KB
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
import socket
import threading
username=input("Enter your name: ")
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 5555
Host='127.0.0.1'
client.connect((Host, port))
print('Succesfully connected..!')
print("Users Online At the moment: \n")
def receive(client):
while True:
try:
# receiving the messages from the server
message = client.recv(1024).decode('utf-8')
if message == 'Username':
client.send(username.encode('utf-8'))
else:
print(message,end="\n")
except:
# if the server leaves, then close the connection
print("An error has occured the host might be down at the moment please try again!")
client.close()
break
# sending the messages to the server
def senddata(client):
while True:
print('\n')
print('{}:'.format(str(username)))
message = '{}:{}'.format(username, input(''))
client.send(message.encode('utf-8'))
print("\nEnter the name of the person you want to send message: ")
message=input('')
client.send(message.encode('utf-8'))
# thread is created to receive and send data
threading._start_new_thread(receive, (client,))
threading._start_new_thread(senddata, (client,))