How to identify tcp connection used by Curl?

My goal is to find out whether or not subsequent curl request used the same tcp connection as the initial one. I use pycurl and curl_easy_getinfo. I can see there is a LASTSOCKET in curl_easy_getinfo which is “the last socket used by this curl session”. So I was thinking that the socket would change if there is a new tcp connection used or remain the same if connection has not changed. Also, according to CURLOPT_MAXAGE_CONN the connection cache is 118 seconds by default. So I expect socket to change anyway after 2 minutes. But for some reason the socket number is always the same ‘6’:

import pycurl
from datetime import datetime
import time
url = "some url"

def doit (url):
    m = {}
    c = pycurl.Curl()
    c.setopt(c.URL, url)
    c.setopt(pycurl.WRITEFUNCTION, lambda x: None)
    now =
    current_time = now.strftime("%H:%M:%S")

    #times in milliseconds
    m['timestamp'] = current_time
    m['total_time'] = round(c.getinfo(pycurl.TOTAL_TIME) * 1000, 1)
    m['namelookup_time'] = round(c.getinfo(pycurl.NAMELOOKUP_TIME) * 1000, 1)
    m['connect_time'] = round(c.getinfo(pycurl.CONNECT_TIME) * 1000, 1)
    m['appconnect_time'] = round(c.getinfo(pycurl.APPCONNECT_TIME) * 1000, 1)

    download_speed = c.getinfo(pycurl.SPEED_DOWNLOAD)
    m['download_speed'] = download_speed

    activesocket = c.getinfo(pycurl.LASTSOCKET)
    m['activesocket'] = activesocket


i = 0
itterations = 3
pause = 120

for i in range(0, itterations):
    i += 1
i = 0

print ('Waiting:', pause, ' seconds')
time.sleep (pause)

for i in range(0, itterations):
    i += 1


{'timestamp': '18:49:34', 'total_time': 389.1, 'namelookup_time': 43.4, 'connect_time': 49.0, 'appconnect_time': 80.1, 'download_speed': 146791.0, 'activesocket': 6}
{'timestamp': '18:49:35', 'total_time': 102.2, 'namelookup_time': 1.8, 'connect_time': 13.6, 'appconnect_time': 44.9, 'download_speed': 558842.0, 'activesocket': 6}
{'timestamp': '18:49:35', 'total_time': 70.6, 'namelookup_time': 1.8, 'connect_time': 13.8, 'appconnect_time': 34.1, 'download_speed': 809169.0, 'activesocket': 6}
Waiting: 120  seconds
{'timestamp': '18:51:35', 'total_time': 168.3, 'namelookup_time': 38.6, 'connect_time': 50.2, 'appconnect_time': 94.8, 'download_speed': 339384.0, 'activesocket': 6}
{'timestamp': '18:51:35', 'total_time': 56.3, 'namelookup_time': 1.8, 'connect_time': 8.2, 'appconnect_time': 28.7, 'download_speed': 1014765.0, 'activesocket': 6}
{'timestamp': '18:51:35', 'total_time': 193.0, 'namelookup_time': 1.8, 'connect_time': 6.4, 'appconnect_time': 30.5, 'download_speed': 295883.0, 'activesocket': 6}

I can see the ‘connect_time’ is indeed less in the subsequent requests which is likely due to connection re-use. But why the socket number doesn’t change? And what would be the correct way to identify if Curl re-used the tcp connection and what the connection is (for the sake of verification)? Appreciate any help! Thank you.


But for some reason the socket number is always the same ‘6’ …

The socket number is the file descriptor number. If a file descriptor is closed (like in TCP connection close) and a new one created (like in creating a new TCP socket) it is pretty common that one ends with the same number.

A better indicator is to get the local source port number of the TCP connection using socket.getpeername. The source port gets for a new TCP connection gets assigned more or less randomly and rapidly reusing the same source port is not possible due to how TCP works. So if it is the same source port as before it is likely the same TCP connection, with different source port it is a different connection.