forked from Rishi098/Advance_Python_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
37 lines (31 loc) · 823 Bytes
/
Copy pathclient.py
File metadata and controls
37 lines (31 loc) · 823 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
import threading
import socket
import sys
HOST = '127.0.0.1'
PORT = 12345
def receive_loop(sock):
"""Receive messages from server and print them."""
try:
while True:
data = sock.recv(4096)
if not data:
break
print(data.decode('utf-8'))
except:
pass
finally:
print("Disconnected by server.")
sys.exit()
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
threading.Thread(target=receive_loop, args=(sock,), daemon=True).start()
while True:
msg = input()
if msg.lower() == '/quit':
sock.sendall(msg.encode('utf-8'))
break
sock.sendall(msg.encode('utf-8'))
sock.close()
if __name__ == '__main__':
main()