diff options
| author | pinky <> | 2007-01-23 07:49:58 +0000 |
|---|---|---|
| committer | pinky <> | 2007-01-23 07:49:58 +0000 |
| commit | 4c4d089975b253f3b6113622315d38caf4f1e5ba (patch) | |
| tree | cff3098726589ead536cba1b6522ec2f47f8f216 /bin/tiny_socket.py | |
| parent | 241c669960d6f49181c55f552310a492965df195 (diff) | |
Sockets
bzr revid: pinky-c9c22d87042263a0c2bb4aed72c6063c5c0356b2
Diffstat (limited to 'bin/tiny_socket.py')
| -rw-r--r-- | bin/tiny_socket.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/bin/tiny_socket.py b/bin/tiny_socket.py new file mode 100644 index 00000000000..3585ccae295 --- /dev/null +++ b/bin/tiny_socket.py @@ -0,0 +1,32 @@ +import socket +import cPickle +import marshal + +class mysocket: + 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 mysend(self, msg): + msg = cPickle.dumps(msg) + size = len(msg) + self.sock.send('%8d' % size) + totalsent = 0 + while totalsent < size: + sent = self.sock.send(msg[totalsent:]) + if sent == 0: + raise RuntimeError, "socket connection broken" + totalsent = totalsent + sent + def myreceive(self): + size = int(self.sock.recv(8)) + msg = '' + while len(msg) < size: + chunk = self.sock.recv(size-len(msg)) + if chunk == '': + raise RuntimeError, "socket connection broken" + msg = msg + chunk + return cPickle.loads(msg) |
