Line | |
---|
1 | ###############################################################################
|
---|
2 | # The communication module (communication.py)
|
---|
3 | ###############################################################################
|
---|
4 | import cPickle
|
---|
5 | import socket
|
---|
6 | import struct
|
---|
7 |
|
---|
8 | marshall = cPickle.dumps
|
---|
9 | unmarshall = cPickle.loads
|
---|
10 |
|
---|
11 | def send(channel, *args):
|
---|
12 | buf = marshall(args)
|
---|
13 | value = socket.htonl(len(buf))
|
---|
14 | size = struct.pack("L",value)
|
---|
15 | channel.send(size)
|
---|
16 | channel.send(buf)
|
---|
17 |
|
---|
18 | def receive(channel):
|
---|
19 |
|
---|
20 | size = struct.calcsize("L")
|
---|
21 | size = channel.recv(size)
|
---|
22 | try:
|
---|
23 | size = socket.ntohl(struct.unpack("L", size)[0])
|
---|
24 | except struct.error, e:
|
---|
25 | return ''
|
---|
26 |
|
---|
27 | buf = ""
|
---|
28 |
|
---|
29 | while len(buf) < size:
|
---|
30 | buf = channel.recv(size - len(buf))
|
---|
31 |
|
---|
32 | return unmarshall(buf)[0]
|
---|
Note:
See
TracBrowser
for help on using the repository browser.