forked from techwithtim/Python-Socket-Chat-App
-
Notifications
You must be signed in to change notification settings - Fork 56
/
client.py
41 lines (28 loc) · 761 Bytes
/
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
import socket
import time
PORT = 5050
SERVER = "localhost"
ADDR = (SERVER, PORT)
FORMAT = "utf-8"
DISCONNECT_MESSAGE = "!DISCONNECT"
def connect():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
return client
def send(client, msg):
message = msg.encode(FORMAT)
client.send(message)
def start():
answer = input('Would you like to connect (yes/no)? ')
if answer.lower() != 'yes':
return
connection = connect()
while True:
msg = input("Message (q for quit): ")
if msg == 'q':
break
send(connection, msg)
send(connection, DISCONNECT_MESSAGE)
time.sleep(1)
print('Disconnected')
start()