-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
37 lines (29 loc) · 961 Bytes
/
Client.py
File metadata and controls
37 lines (29 loc) · 961 Bytes
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
from socket import *
import threading
# Starts the client. Connects to the server
def runClient():
server_name = ''
server_port = 12000
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect((server_name, server_port))
print("Connected. Enter you message to send: ('.exit' to exit)")
threading.Thread(target=recvMsg, args=[client_socket]).start() # Starts a new thread to receive messages
sendMsg(client_socket)
client_socket.close()
# Sends message to server
def sendMsg(client_socket):
msg = ""
while msg != ".exit":
msg = input()
client_socket.send(str.encode(msg))
# Receives messages from the server
def recvMsg(client_socket):
try:
while True:
recvdMsg = client_socket.recv(1024)
plainTextMsg = recvdMsg.decode()
print(plainTextMsg)
except:
print("Client closed")
if __name__ == "__main__":
runClient()