Skip to content

Instantly share code, notes, and snippets.

@diogobaeder
Created February 19, 2013 01:49
Show Gist options
  • Save diogobaeder/4982425 to your computer and use it in GitHub Desktop.
Save diogobaeder/4982425 to your computer and use it in GitHub Desktop.
Test with Memcached multi-get
#!/usr/bin/env python
import binascii
import socket
blocks = '''
800d00030000000000000003000000000000000000000000666f6f
800d00040000000000000004000000000000000000000000666f6f32
800a00000000000000000000000000000000000000000000
810d0003040000000000000a00000000000000000000000800000000666f6f626172
810d0004040000000000000c00000000000000000000000700000000666f6f3262617232
810a00000000000000000000000000000000000000000000
'''.strip().split('\n')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 11211))
s.sendall(binascii.unhexlify(blocks[0]))
s.sendall(binascii.unhexlify(blocks[1]))
s.sendall(binascii.unhexlify(blocks[2]))
def receive_block(index):
block = blocks[index]
block_length = len(binascii.unhexlify(block))
message = s.recv(block_length)
return repr(message)
print 'received:', receive_block(3)
print 'received:', receive_block(4)
print 'received:', receive_block(5)
print 'finished'
@diogobaeder
Copy link
Author

Result:

$ ▶./multiget_test.py 
received: '\x81\r\x00\x03\x04\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00foobar'
received: '\x81\r\x00\x04\x04\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\t\x00\x00\x00\x00foo2bar2'
received: '\x81\n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
finished

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment