-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocketHandler.py
More file actions
36 lines (28 loc) · 786 Bytes
/
Copy pathSocketHandler.py
File metadata and controls
36 lines (28 loc) · 786 Bytes
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
import socket
class SocketHandler:
def __init__(self, sock=None):
if sock is None:
self.sock = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
def connect(self, host, port):
self.sock.connect((host, port))
def send(self, msg):
totalsent = 0
MSGLEN = len(msg)
while totalsent < MSGLEN:
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError("socket connection broken")
totalsent = totalsent + sent
def receive(self, EOFChar='\036'):
msg = ''
MSGLEN = 100
while len(msg) < MSGLEN:
chunk = self.sock.recv(MSGLEN-len(msg))
if chunk.find(EOFChar) != -1:
msg = msg + chunk
return msg
msg = msg + chunk
return msg