如何制作具有相同会话 ID 的 HTTP Post?

How to make an HTTP Post with the same session ID?

如何使用当前会话 ID 和 HTTPS 连接发出 HTTP Post 请求?

目前,我是:

def postData(product, url, dataToPost): 
    ret_status = False
    session = requests.session()
    try :
        # Open session
        response = session.post(product.get_url("login.htm"), verify = False, data = {"user": product.login, "password": product.password}, timeout = 5000)

        # Send HTTP Post
        response = session.post(product.get_url(url), verify = False, data = dataToPost, timeout = 5000)
        dataJson = json.loads(response.text)
        if(dataJson['status'] == "OK"):
            ret_status = True

        # Close session
        response = session.post(product.get_url("login.htm?logout"), verify = False, timeout = 5000)

    except TimeoutException:
        print("         [timeout] HTTP->sendCmd")
        session.close()
        return False
    except requests.exceptions.SSLError as sslErr:
        print("         [SSLError] HTTP->sendCmd")
        session.close()
        return False

    session.close()
    return ret_status 

我的测试:

# Open session
driver.get(product.get_url("login.htm"))
wait.until(EC.visibility_of_element_located((By.ID, "loginInput")))
element = driver.find_element(By.ID, "loginInput")
element.send_keys(login)
element = driver.find_element(By.ID, "passInput")
element.send_keys(password)
driver.find_element(By.ID, "submitButton").click()

# Wait index.htm is loaded
wait.until(EC.visibility_of_element_located((By.ID, "index_content")))

# Do some test ...

# Send HTTP Post
postData(product, "toto.json", {"key1":"value1"})

# Do some test ...

# Do some test ...



# Close session
driver.get(product.get_url("login.htm?logout"))

问题是当我调用 postData() 时,它打开了一个新会话...我想使用相同的 HTTPS 连接和会话。 我该怎么办?

我的网络服务器在嵌入式系统中,这就是为什么我需要限制并发 HTTPS 连接数的原因

只需在顶层创建会话并将其传递给 postData():

def postData(product, url, dataToPost, session):
    # use session here
session = requests.session()

# use session here

# Send HTTP Post
postData(product, "toto.json", {"key1":"value1"}, session)

# use session here

session.close()