summaryrefslogtreecommitdiff
path: root/bin/tiny_socket.py
diff options
context:
space:
mode:
authorpinky <>2007-01-23 07:49:58 +0000
committerpinky <>2007-01-23 07:49:58 +0000
commit4c4d089975b253f3b6113622315d38caf4f1e5ba (patch)
treecff3098726589ead536cba1b6522ec2f47f8f216 /bin/tiny_socket.py
parent241c669960d6f49181c55f552310a492965df195 (diff)
Sockets
bzr revid: pinky-c9c22d87042263a0c2bb4aed72c6063c5c0356b2
Diffstat (limited to 'bin/tiny_socket.py')
-rw-r--r--bin/tiny_socket.py32
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)