-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathchessnet.py
More file actions
75 lines (61 loc) · 2.21 KB
/
chessnet.py
File metadata and controls
75 lines (61 loc) · 2.21 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# pycchess - just another chinese chess UI
# Copyright (C) 2011 - 2015 timebug
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import socket, sys, traceback
class chessnet():
def __init__(self):
self.host = ''
self.NET_HOST = ''
self.NET_PORT = 62222
def send_move(self, move):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((self.NET_HOST, self.NET_PORT))
except socket.error, e:
print "Couldn't find your port: %s" % e
sys.exit(1)
try:
s.send(move)
except socket.error, e:
print "Error sending data (detected by shutdown): %s" % e
sys.exit(1)
s.close()
def get_move(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOCK_STREAM, socket.SO_REUSEADDR, 1)
s.bind((self.host, self.NET_PORT))
s.listen(1)
while 1:
try:
clientsock, clientaddr = s.accept()
# print clientaddr
except KeyboardInterrupt:
raise
except:
traceback.print_exc()
continue
try:
move = clientsock.recv(1024)
except socket.error, e:
print "Error receiving data: %s" % e
sys.exit(1)
except:
traceback.print_exc()
try:
clientsock.close()
except KeyboardInterrupt:
raise
except:
traceback.print_exc()
return move