-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
56 lines (49 loc) · 1.48 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
55
56
import socket
from _thread import *
import sys
serverPort = 9999
try:
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as e:
print("Create socket failed: %s" % e)
sys.exit(1)
try:
clientSocket.connect(('localhost', serverPort))
except socket.gaierror as e:
print("Address-related error: %s" % e)
sys.exit(1)
except socket.error as e:
print("Error connecting to server: %s" % e)
sys.exit(1)
print("Server Connection Accepted")
try:
print("You are: " + clientSocket.recv(2048).decode('utf-8'))
clientSocket.send("OK".encode('utf-8'))
except socket.error as e:
print(e)
while True:
try:
data = clientSocket.recv(2048).decode('utf-8')
if (data == "O Won" or data == "X Won" or data == "We Tied"):
print(data)
datafinish = clientSocket.recv(2048).decode('utf-8')
datafinish = datafinish.split('/')
print(datafinish[0:3])
print(datafinish[3:6])
print(datafinish[6:9])
break
data = data.split('/')
print(data[0:3])
print(data[3:6])
print(data[6:9])
while True:
play = int(input("Choose your move (1-9): "))
if data[play-1] == '-':
clientSocket.send(str(play).encode('utf-8'))
break
else:
print("Invalid Move")
except socket.error as e:
print(e)
clientSocket.close()
print("Server Connection closed")