-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
56 lines (36 loc) · 1.15 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
from threading import Thread
import socket
import sys
def handle_printing(socket):
while True:
received_msg = socket.recv(1024).decode()
received_msg = str(received_msg)
if received_msg == "PING":
socket.send("PONG".encode())
if "your message published successfully" in received_msg:
socket.close()
break
for line in received_msg.splitlines():
print(f"> {line}")
def runner():
print(sys.argv)
_, host, port, *user_input = sys.argv
# Create a socket object
s = socket.socket()
try:
s.connect((host, int(port)))
# s.connect(('127.0.0.1', 12345))
except ConnectionRefusedError:
print("Connection refused")
return
except Exception as e:
print(e)
print("Some error occured...")
return
print("client connected to server successfully!")
text = " ".join(user_input)
s.send(text.encode())
thread = Thread(target = handle_printing, args = (s, ))
thread.start()
# s.close()
runner()