MCPcopy Create free account
hub / github.com/ActiveState/code / serveclient

Function serveclient

recipes/Python/577569_NBD_server_in_python/recipe-577569.py:12–37  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10 return ''.join(rv)
11
12def serveclient():
13 READ, WRITE, CLOSE = 0,1,2
14 "Serves a single client until it exits."
15 afile.seek(0, 2)
16 asock.send('NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53' + struct.pack('>Q', afile.tell()) + '\0'*128);
17 while True:
18 header = recvall(asock, struct.calcsize('>LL8sQL'))
19 magic, request, handle, offset, dlen = struct.unpack('>LL8sQL', header)
20 assert magic == 0x25609513
21 if request == READ:
22 afile.seek(offset)
23 asock.send('gDf\x98\0\0\0\0'+handle)
24 asock.send(afile.read(dlen))
25 print "read\t0x%08x\t0x%08x" % (offset, dlen), time.time()
26 elif request == WRITE:
27 afile.seek(offset)
28 afile.write(recvall(asock, dlen))
29 afile.flush()
30 asock.send('gDf\x98\0\0\0\0'+handle)
31 print "write\t0x%08x\t0x%08x" % (offset, dlen), time.time()
32 elif request == CLOSE:
33 asock.close()
34 print "closed"
35 return
36 else:
37 print "ignored request", request
38
39if __name__ == '__main__':
40 "Given a port and a filename, serves up the file."

Callers 1

recipe-577569.pyFile · 0.85

Calls 11

recvallFunction · 0.70
seekMethod · 0.45
sendMethod · 0.45
packMethod · 0.45
tellMethod · 0.45
unpackMethod · 0.45
readMethod · 0.45
timeMethod · 0.45
writeMethod · 0.45
flushMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected