You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

38 lines
1.1 KiB

import socket
class netpipe:
'''NetPipe: Sends data from one place to another'''
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):
msg = ("%04d" % len(msg)) + msg
#print "np->: "+msg
totalsent = 0
while totalsent < len(msg):
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise SocketClosed, "socket connection broken"
totalsent = totalsent + sent
def receive(self):
length = int(self.sock.recv(4))
msg = ''
while len(msg) < length:
line = self.sock.recv(length - len(msg))
if line == '':
raise SocketClosed, "socket connection broken"
msg = msg + line
#print "np<-: " + msg
return msg
def close(self):
self.sock.close()