Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can curl be used to mimic a persistent (but idle) open connection

I am having trouble with some Http Client connections returning NoHttpResponseException about 1 in 3 times.

My hypothesis is that the firewall is closing idle connections (not unusual). I would like to use curl to mimic this.

I have been trying something like this but very likely the connection never goes idle

curl --keepalive-time 60 --limit-rate 128B $URL $URL $URL $URL $URL $URL $URL $URL $URL $URL $URL $URL $URL $URL

What I would like is something akin to

curl $URL
sleep 10
curl $URL    
sleep 20
curl $URL 
sleep 30
curl $URL 

Where each time (using some magic!) Curl reuses the same connection.

Is there any way to do this using curl (or wget) and not have to write a program to do it

like image 832
kellyfj Avatar asked Jan 15 '13 13:01

kellyfj


People also ask

Does Curl reuse connection?

curl will always try to keep connections alive and reuse existing connections as far as it can. Connections are kept in the connection pool, sometimes also called the connection cache.

Does Curl close connection?

The Curl client can tell the server that it wants to close the connection by sending the "Connection: close" header to the server. To pass the "Connection: close" header to Curl, you can use the -H command-line option.

How does HTTP keep alive work?

HTTP keep-alive, a.k.a., HTTP persistent connection, is an instruction that allows a single TCP connection to remain open for multiple HTTP requests/responses. By default, HTTP connections close after each request.


2 Answers

There is no such thing you couldn't do with netcat.

REQ="GET / HTTP/1.1\nHost: localhost\nAccept: *\n\n"
(echo -ne $REQ; sleep 1; echo -ne $REQ; sleep 2; echo -ne $REQ) | netcat localhost 80

Change the path to something different than / if you need it...

like image 200
Marek Kowalski Avatar answered Sep 17 '22 17:09

Marek Kowalski


No, there's no way for neither curl nor wget to do this. When the tool exits, it will close all connections.

I'd say that the best option for you is to use a scripting language and perhaps a libcurl binding that allows you do to all the curl magic in a more programmatic way. That way you can do a request, sleep 10 seconds and then do another request and it will then re-use the same connection - as far as possible.

There are libcurl bindings available for pretty much any programming language of your choice, and even when not using libcurl you can probably achieve this functionality although perhaps not with the same ease.

like image 38
Daniel Stenberg Avatar answered Sep 18 '22 17:09

Daniel Stenberg