-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketWrapper.py
More file actions
61 lines (49 loc) · 1.35 KB
/
SocketWrapper.py
File metadata and controls
61 lines (49 loc) · 1.35 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
import socket
def socketCreation():
try:
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
except socket.error as err:
print "failed to create socket: ", err
else:
return sock
def socketBind(sock, host, port):
try:
sock.bind((host,port))
except socket.error as err:
print "failed to bind address: ", err
def socketListen(sock):
try:
sock.listen(5)
except socket.error as err:
print "failed to listen socket: ", err
def socketAccept(sock):
try:
s, a = sock.accept()
except socket.error as err:
print "failed to accept client: ", err
else:
return s, a
def socketConnection(sock, host, port):
try:
sock.connect((host,port))
except socket.gaierror as err:
print "failed to connect to server: ", err
def socketSend(sock, data):
# we add EOD as the segmentation of data stream
data += 'EOD'
try:
sock.send(data)
except socket.error as err:
print "failed to send data: ", err
def socketRecv(sock, recvBuffSize):
data = ''
while 1:
try:
buf = sock.recv(recvBuffSize)
except socket.error as err:
print "failed to receive data", err
else:
data = data + buf
if data[-3:] == 'EOD':
break
return data[:-3]