How do you end the connection? (python api to insight vm cloud)

This is my code, but i noticed it doesnt turn off or end and keeps looping, what paramater can i use to tell it to stop?

from curses import raw, reset_shell_mode
from http import server
from turtle import pd
import requests, sys
import urllib3
from pathlib import Path  
import json
from datetime import datetime
from requests import Session
from typing import Any, List, Tuple, Dict
import pandas as pd
from requests.adapters import HTTPAdapter, Retry

# disable urllib3 warnings for SSL
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
sys.path.append('/Users/s153152/Documents/Workspace/kpi/rapid7')


def _headers():
    headers = {
            'X-Api-Key': 'insert API key here',
            'Content-type':'application/json', 
            'Accept':'*/*'
            }
    return headers

def _request():

    third_party_patching_filer = {
    "asset": "asset.tags IN ['osswin'] && asset.os.vendor CONTAINS 'microsoft' && asset.agentKey IS NOT NULL", 
    "vulnerability" : "vulnerability.categories NOT IN ['microsoft patch']"}

    headers = _headers()
    print(headers)
    url1 = f"https://us.api.insight.rapid7.com/vm/v4/integration/assets"
    resp = requests.post(url=url1, headers=headers, json=third_party_patching_filer, verify=False).json()

    has_next_cursor = True
    nextKey = ""
    
    if "cursor" in resp["metadata"]:
        has_next_cursor = True
        nextKey = resp["metadata"]["cursor"]

    total_server_ip_addresses = []
    osname_server1 = []
    total_critical_vul_osswin = []
    results = []

    with requests.Session() as session:
        url2 = f"https://us.api.insight.rapid7.com/vm/v4/integration/assets?&size=50&cursor={nextKey}"
        while has_next_cursor:

            s = requests.Session()
            backoff_factor=0.3
            retries = Retry(total=10, backoff_factor=backoff_factor, status_forcelist=[ 502, 503, 504 ])
            s.mount(url2, HTTPAdapter(max_retries=retries))
            s = s.post(url=url2, headers=headers, json=third_party_patching_filer, verify=False)
            print(s.status_code)
            s = s.json()
            print(s)

            cursor = s["metadata"]
            print(cursor)
            if "cursor" in cursor:

                nextKey = cursor["cursor"]
                print(f"next key {nextKey}")
                #print(desktop_support)
                for data in s["data"]:
                    for tags in data['tags']:
                        
                        if tags["name"] == 'OSSWIN':
                            try:
                                osname_server = data['host_name']
                                server_host_ip_address = data['ip']
                                critical_vuln_osswin = data['critical_vulnerabilities']
                                os_type = data['os_type']
                                data = {
                                    'ip_address': server_host_ip_address,
                                    'os_name': osname_server,
                                    'critical_vuln': critical_vuln_osswin,
                                    'os_type': os_type
                                }
                                results.append(data)
                            except Exception as e:
                                f"Possibly no ip address, error {e}"
                            #print(f"Server asset: {osname_server1}, {total_server_ip_addresses}")
                            
                            with open('server_info.json', 'w') as f:
                                json.dump(results, f, indent=2)

                            #print(data['host_name'])
                            print(sum(total_critical_vul_osswin))

            else:
                has_next_cursor = False
        filepath = Path(archive_name='out.csv')
        filepath.parent.mkdir(parents=True, exist_ok=True)  
        df = pd.DataFrame(total_server_ip_addresses)
        df.to_csv(filepath)


    return total_server_ip_addresses, sum(total_server_ip_addresses)

def smart_filter():
    test = _request()
    print(test)


smart_filter()