Python 多个 PATCH 给出 http.client.CannotSendRequest:请求已发送

Python multiple PATCH gives http.client.CannotSendRequest: Request-sent

当我尝试使用 Zoom API 更新用户类型(基本或许可)时遇到问题。 我需要自动向一组用户授予许可证。 它仅适用于第一个用户,当我尝试更新第二个用户时,出现此错误:

Traceback (most recent call last):
  File "C:\Users\dmv\Documents\ZoomLicenseProj\Test\LicenseManager.py", line 58, in <module>
    conn.request("PATCH", url_update, headers=headers, body=params)
  File "C:\Users\dmv\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\dmv\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1266, in _send_request
    self.putrequest(method, url, **skips)
  File "C:\Users\dmv\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1092, in putrequest
    raise CannotSendRequest(self.__state)
http.client.CannotSendRequest: Request-sent

我不明白为什么它只适用于第一个,然后就崩溃了。我认为没有用,但我什至尝试在每次通话之间睡 2 分钟,同样的问题。 这是我的代码:

import sys
import jwt
import http.client
import datetime
import json

# Go to 
# Then get API Key, API Secret and insert below
api_key = 'XXX'
api_sec = 'XXXXXX'

payload = {
'iss':api_key,
'exp': datetime.datetime.now() + datetime.timedelta(hours=2)
}

jwt_encoded = str(jwt.encode(payload, api_sec), 'utf-8')

conn = http.client.HTTPSConnection("api.zoom.us")

headers = {
'authorization': "Bearer %s" % jwt_encoded,
'content-type': "application/json"
}
licensedMemberUpdate = {'type' : '2'}
baseMemberUpdate = {'type' : '1'}

conn.request("GET", "/v2/groups", headers=headers)

res = conn.getresponse()
response_string = res.read().decode('utf-8')
response_obj = json.loads(response_string)

if 'groups' in response_obj:
    groups = response_obj['groups']
    for group in groups:
        if 'Test_LicenseManager' in group['name']:
            group_id = group['id']
            
            conn.request("GET", "/v2/groups/" + group_id + "/members", headers=headers)
            res = conn.getresponse()
            response_string = res.read().decode('utf-8')
            members_list = json.loads(response_string)['members']
            
            json_lic = json.dumps(licensedMemberUpdate)
            json_base = json.dumps(baseMemberUpdate)
            
            for member in members_list:
                url_update = "/v2/users/%s" % member['email']
                
                conn.request("PATCH", url_update, headers=headers, body=json_base)
                
                #res = conn.getresponse()
                #response_obj = res.status

来自 http.client 文档:

An HTTPConnection instance represents one transaction with an HTTP server.

由于我无法使用缩放 API,我使用对 google.com:

的简单获取请求测试了行为
import http.client

conn = http.client.HTTPSConnection("google.com")

for _ in range(10):
    conn.request("GET", "/")
    res = conn.getresponse()

导致与您相同的错误:http.client.ResponseNotReady: Request-sent。 相反,为每个请求打开一个新连接:

import http.client

for _ in range(10):
    conn = http.client.HTTPSConnection("google.com")
    conn.request("GET", "/")
    res = conn.getresponse()

作为替代方案,我(以及 http.client 的作者)建议使用 requests 作为高级 HTTP 客户端,它有很好的文档记录且易于使用。