-
-
Save maurobaraldi/f53757e12953f146085ad49a4bc184b4 to your computer and use it in GitHub Desktop.
Simple use socket connect redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import socket, time | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect(('localhost', 6379)) | |
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) | |
time.sleep(2) | |
sock.sendall('PING\r\n') | |
print repr(sock.recv(4096)) | |
time.sleep(2) | |
sock.sendall('get for\r\n') | |
print repr(sock.recv(4096)) | |
time.sleep(2) | |
sock.sendall('lrange mylist 0 -1\r\n') | |
print repr(sock.recv(4096)) | |
sock.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import socket, time | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect(('localhost', 6379)) | |
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) | |
fp = sock.makefile('r') | |
sock.sendall('PING\r\n') | |
print fp.readline()[:-2] | |
time.sleep(2) | |
fp = sock.makefile('r') | |
sock.sendall('EXISTS mylist\r\n') | |
print fp.readline()[:-2] | |
time.sleep(2) | |
fp = sock.makefile('r') | |
sock.sendall('get for\r\n') | |
print fp.read(int(fp.readline()[1:-2])) | |
time.sleep(2) | |
fp = sock.makefile('r') | |
sock.sendall('lrange mylist 0 -1\r\n') | |
n = int(fp.readline()[1:-2]) | |
#print n | |
for i in range(n): | |
response = fp.readline()[:-2][1:] | |
l = 0 if len(response) == 0 else int(response) | |
#print l, '--', response | |
print l and fp.read(l) or '' | |
fp.read(2) | |
fp = sock.makefile('r') | |
sock.sendall('sort mylist\r\n') | |
n = int(fp.readline()[1:-2]) | |
#print n | |
for i in range(n): | |
response = fp.readline()[:-2][1:] | |
l = 0 if len(response) == 0 else int(response) | |
print l and fp.read(l) or '' | |
fp.read(2) | |
fp = None | |
sock.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment