1- import socket
1+ from glide import GlideClient , NodeAddress
22
33from testcontainers .valkey import ValkeyContainer
44
@@ -13,24 +13,22 @@ def basic_example():
1313 print (f"Valkey connection URL: { connection_url } " )
1414 print (f"Host: { host } , Port: { port } " )
1515
16- # Connect using raw socket and RESP protocol
17- with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
18- s .connect ((host , port ))
16+ # Connect using Glide client
17+ client = GlideClient ([NodeAddress (host , port )])
1918
20- # PING command
21- s .sendall (b"*1\r \n $4\r \n PING\r \n " )
22- response = s .recv (1024 )
23- print (f"PING response: { response .decode ()} " )
19+ # PING command
20+ pong = client .ping ()
21+ print (f"PING response: { pong } " )
2422
25- # SET command
26- s .sendall (b"*3\r \n $3\r \n SET\r \n $3\r \n key\r \n $5\r \n value\r \n " )
27- response = s .recv (1024 )
28- print (f"SET response: { response .decode ()} " )
23+ # SET command
24+ client .set ("key" , "value" )
25+ print ("SET response: OK" )
2926
30- # GET command
31- s .sendall (b"*2\r \n $3\r \n GET\r \n $3\r \n key\r \n " )
32- response = s .recv (1024 )
33- print (f"GET response: { response .decode ()} " )
27+ # GET command
28+ value = client .get ("key" )
29+ print (f"GET response: { value } " )
30+
31+ client .close ()
3432
3533
3634def password_example ():
@@ -41,18 +39,14 @@ def password_example():
4139
4240 print (f"\n Valkey with password connection URL: { connection_url } " )
4341
44- with socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) as s :
45- s . connect ( (host , port ))
42+ # Connect using Glide client with password
43+ client = GlideClient ([ NodeAddress (host , port )], password = "mypassword" )
4644
47- # AUTH command
48- s .sendall (b"*2\r \n $4\r \n AUTH\r \n $10\r \n mypassword\r \n " )
49- response = s .recv (1024 )
50- print (f"AUTH response: { response .decode ()} " )
45+ # PING after auth
46+ pong = client .ping ()
47+ print (f"PING response: { pong } " )
5148
52- # PING after auth
53- s .sendall (b"*1\r \n $4\r \n PING\r \n " )
54- response = s .recv (1024 )
55- print (f"PING response: { response .decode ()} " )
49+ client .close ()
5650
5751
5852def version_example ():
@@ -70,11 +64,11 @@ def bundle_example():
7064 host = valkey_container .get_host ()
7165 port = valkey_container .get_exposed_port ()
7266
73- with socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) as s :
74- s . connect ( (host , port ))
75- s . sendall ( b"*1 \r \n $4 \r \n PING \r \n " )
76- response = s . recv ( 1024 )
77- print ( f"PING response: { response . decode () } " )
67+ # Connect using Glide client
68+ client = GlideClient ([ NodeAddress (host , port )] )
69+ pong = client . ping ( )
70+ print ( f"PING response: { pong } " )
71+ client . close ( )
7872
7973
8074if __name__ == "__main__" :
0 commit comments