Python requests session keeps creating connection without reusing the connection
Question: I have the following code:

    
    
    protocol = "http"
    if self.protocol == PROTOCOL_HTTPS:
      protocol = "https"
    if self.session is None:
      self.session = Session()
      self.session.get(protocol+'://'+self.ip)
    url = protocol+"://"+self.ip+requestURL
    response = None
    if requestType == GET_METHOD:
      response = self.session.get(url, headers=(header),stream=False)
    elif requestType == POST_METHOD:
      response = self.session.post(url, payload, headers=(header), stream=False)
    

This code works but it opens too many connections to the device. I want only
one connection to be opened in the session. I tried the following code but it
doesn't seem to work. It is still creating more than 1 connection.

    
    
    adapter = requests.adapters.HTTPAdapter(pool_connections=1, pool_maxsize=1)
    self.session.mount('http://', adapter)
    url = protocol+"://"+self.ip+requestURL
    resp = self.session.get(url)
    

What am I doing wrong?

How do I ensure that only one connection is opened in the session?


Answer: You don't need to use an `HTTPAdapter` for one connection. May just try

    
    
    >>> import requests
    >>> s = requests.Session()
    >>> s.get('http://httpbin.org/get')
    

from <http://docs.python-requests.org/en/latest/api/#requests.Session>

