user1437328
user1437328

Reputation: 15846

Why netcat doesn't dumps the response?

$ cat mass_insert.txt 
*3
$3
SET
$4
key1
$6
value1
*3
$3
SET
$4
key2
$6
value2
$ cat mass_insert.txt | nc localhost 6379

If you see, I've redis commands for mass insert in a file mass_insert.text, I cat it to netcat and the commands get sent fine to redis but the response is not dumped. The question is WHY ?

A simple google.com HEAD request with netcat works just fine, i.e., dumps the response -

$ printf "HEAD / HTTP/1.0\r\n\r\n" | nc google.com 80
HTTP/1.0 302 Found
Location: http://www.google.co.in/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=cac6fac8de9b5d0c:FF=0:TM=1365352115:LM=1365352115:S=CVuSzOK8mrsfFczI; expires=Tue, 07-Apr-2015 16:28:35 GMT; path=/; domain=.google.com
Set-Cookie: NID=67=K5KjAFxuDP5epZKDdvSf4oCe4FRP1AL8LG_9MEdsArn4Oz_UBG69H0KOMUiKLuVN-hH4NIUvHOLgmfh_82P5v6Nh-sGFrGXyCnQz0zD-Sj_QbzxQ6NIK0rahsxjvyTn7; expires=Mon, 07-Oct-2013 16:28:35 GMT; path=/; domain=.google.com; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Date: Sun, 07 Apr 2013 16:28:35 GMT
Server: gws
Content-Length: 221
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

Upvotes: 4

Views: 4970

Answers (1)

Didier Spezia
Didier Spezia

Reputation: 73246

You may want to check the following points.

First, the line separator in Redis protocol is \r\n (and not just \n). So you need to be sure that your input file includes those characters.

Then, netcat shutdowns the connection at the end of the input file (it may therefore not wait for a Redis reply). It probably depends on the version of netcat though. On my system:

$ od -c toto.txt
0000000   *   3  \n   $   3  \n   S   E   T  \n   $   4  \n   k   e   y
0000020   1  \n   $   6  \n   v   a   l   u   e   1  \n   *   3  \n   $
0000040   3  \n   S   E   T  \n   $   4  \n   k   e   y   2  \n   $   6
0000060  \n   v   a   l   u   e   2  \n

$ ( sed 's/$/\r/' < toto.txt ; sleep 1 ) | netcat localhost 6379
+OK
+OK

The extra second gives netcat a chance to read Redis reply.

Note that redis-cli in --pipe mode is much better suited than netcat to perform massive injections through the Redis protocol.

Upvotes: 6

Related Questions