-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
42 lines (31 loc) · 1.22 KB
/
client.py
File metadata and controls
42 lines (31 loc) · 1.22 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
import socket
import threading
# Where to open this socket
_HOST = "127.0.0.1"
_PORT = 4000
# Try to connect to the server
server = socket.socket()
x = server.connect((_HOST, _PORT))
print("Connected to socket server")
# Function which will continually listen for any inputs from the user, and forward them to the server
# This will be run in a separate thread in the background, so it doesn't block the listener below
def inputThread():
global server
while True:
msg = input()
server.send(msg.encode())
# Create and start a thread for that function
# Marks the thread as a daemon so it exits with the main program, just for this tutorial
# There's good reasons why this isn't always the best solution. If you're going to be using threads, Google why this is the case
_listenThread = threading.Thread(target = inputThread, daemon = True)
_listenThread.start()
# Continually listen for any messages from the server
while True:
data = server.recv(1024).decode()
# If the connection is lost, close the socket, and exit the program
if(len(data) == 0):
print("Connection lost...")
server.close()
exit()
# Print out anything we receive from the server
print("> {}".format(data))