简体   繁体   English

为什么 python 补丁请求不适用于循环?

[英]Why doesn't a python patch request work for a loop?

I have a code in which I want to make a patch request for every user (see in "for user in users").我有一个代码,我想在其中为每个用户发出补丁请求(请参阅“for user in users”)。 the output of the print inside the loop is this :循环内打印的输出是这样的:

5ee5eee85cb7558196e1f742 5ee5eee85cb7558196e1f742

101 101

5ee5eee85cb7558196e1f742 5ee5eee85cb7558196e1f742

108 108

5ef9a4ec59bd2a2aabddd13d 5ef9a4ec59bd2a2aabddd13d

242 242

5ee5eee85cb7558196e1f742 5ee5eee85cb7558196e1f742

122 122

5ee5eee85cb7558196e1f742 5ee5eee85cb7558196e1f742

129 129

5ef9a4ec59bd2a2aabddd13d 5ef9a4ec59bd2a2aabddd13d

263 263

5ee5eee85cb7558196e1f742 5ee5eee85cb7558196e1f742

143 143

5ee5eee85cb7558196e1f742 5ee5eee85cb7558196e1f742

150 150

5ef9a4ec59bd2a2aabddd13d 5ef9a4ec59bd2a2aabddd13d

284 284

But the patch is only made only one time and only for the first "5ee5eee85cb7558196e1f742" and the first "5ef9a4ec59bd2a2aabddd13d" and not for all the other times in the loop.但是补丁只制作一次,并且只针对第一个“5ee5eee85cb7558196e1f742”和第一个“5ef9a4ec59bd2a2aabddd13d”,而不是循环中的所有其他时间。 Could someone help me?有人可以帮助我吗?

extrapoints=0    
for position in positions:
    
    
    position_type = position['position_type']
        #an kapoiois user stathmeusi
    if position_type == "parking":
            #kratame tis suntetagmenes thesis gia na doume an thn exei anaferei kapoios reporter    
        coordinatesx = position['location']['coordinates'][0]
        coordinatesy = position['location']['coordinates'][1]  
            #pame sti sullogi reports na doume an uparxoun oi suntetagmenes se kapoia anafora
        for report in reports:
                #rid = report['reporter_id']
            rcoordinatesy = report['location']['coordinates'][1]    
            rcoordinatesx = report['location']['coordinates'][0]
            #extrapoints = 0   
            if rcoordinatesx==coordinatesx and rcoordinatesy==coordinatesy:
                    #an kapoia anafora exei tetoies suntetagmenes prosthetoume 7 pontous ston reporter 
                    #rid = report['reporter_id']
                rid= report['reporter_id']
                extrapoints = extrapoints + 7
                
                
                for user in users:
                    userid = user['_id']
                    rewarding_p = user['rewarding_points']
                    if userid == rid:
                        
                       
                        etag = user['_etag']
                        print (etag)
                        rewarding_p = rewarding_p + extrapoints
                        print(userid)
                        print(rewarding_p)
                        
                        data={"rewarding_points":rewarding_p}
                        headers = {'Authorization':'Bearer 8b59cc74f15befc05b48f90d5e09866028b2b624','If-Match': etag}
                        url = 'https://socialpark.iti.gr:8006/users'
                        new_url = "{}/{}".format(url, userid)
                        r = requests.patch(new_url, data, headers=headers)
                      
                        time.sleep(2)

Here you can see the r content:在这里你可以看到r内容:

{'_content': b'{"_id": "5ee5eee85cb7558196e1f742", "_updated": "Mon, 05 Oct 2020 09:22:22 GMT", "_created": "Sun, 14 Jun 2020 09:33:27 GMT", "_etag": "a97daa90626ed7493010f11d2d4be6ff06e6a7be", "_links": {"self": {"title": "user", "href": "users/5ee5eee85cb7558196e1f742"}}, "_status": "OK"}', '_content_consumed': True, '_next': None, 'status_code': 200, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:22 GMT', 'Content-Type': 'application/json', 'Content-Length': '276', 'Connection': 'keep-alive', 'ETag': '"a97daa90626ed7493010f11d2d4be6ff06e6a7be"', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8E9F68D08>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason': 'OK', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=91716), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA297F88>} {'_content': b'{"_id": "5ee5eee85cb7558196e1f742", "_updated": "Mon, 05 Oct 2020 09:22:22 GMT", "_created": "Sun, 14 Jun 2020 09:33:27 GMT ", "_etag": "a97daa90626ed7493010f11d2d4be6ff06e6a7be", "_links": {"self": {"title": "user", "href": "users/5ee5eee85cb7558196e1f06e6a7be", "_links": "users/5ee5eee85cb7558196e1f},"OKus" '_content_consumed': True, '_next': None, 'status_code': 200, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:22 GMT', '内容类型':'应用程序/json','内容长度':'276','连接':'保持活动','ETag':'“a97daa90626ed7493010f11d2d4be6ff06e6a7be”','Access-Control-Allow-Origin' : '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8E9F68D08>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'en history': [], 'reason': 'OK', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=91716), 'request': <PreparedRequest [PATCH]>, 'connection ': <requests.adapters.HTTPAdapter 对象在 0x000001F8EA297F88>}

ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 115 {'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:24 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8E9FAC6C8>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=102812), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA172088>} ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 115 {'_content': b'{"_status": "ERR", "_error": {"2," 'tsumed_message: {"2," 'tsum_e} 匹配'4 ': True, '_next': 无, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:24 GMT', 'Content-类型':'application/json','Content-Length':'93','Connection':'keep-alive','Access-Control-Allow-Origin':'*'},'raw':<urllib3 .response.HTTPResponse 对象在 0x000001F8E9FAC6C8>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason失败','cookies':<RequestsCookieJar[]>,'elapsed':datetime.timedelta(microseconds=102812),'request':<PreparedRequest [PATCH]>,'connection':<requests.adapters.HTTPAdapter 对象在 0x000001F8EA172088 >}

16b77ead1718639c2ab727aa50c6a8a669a52da9 5ef9a4ec59bd2a2aabddd13d 263 {'_content': b'{"_id": "5ef9a4ec59bd2a2aabddd13d", "_updated": "Mon, 05 Oct 2020 09:22:26 GMT", "_created": "Mon, 29 Jun 2020 08:23:08 GMT", "_etag": "fc0363f6e2b4c59a870c06e057b09c5b94dd44f5", "_links": {"self": {"title": "user", "href": "users/5ef9a4ec59bd2a2aabddd13d"}}, "_status": "OK"}', '_content_consumed': True, '_next': None, 'status_code': 200, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:26 GMT', 'Content-Type': 'application/json', 'Content-Length': '276', 'Connection': 'keep-alive', 'ETag': '"fc0363f6e2b4c59a870c06e057b09c5b94dd44f5"', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8EA031788>, 'url': 'https://socialpark.iti.gr:8006/users/5ef9a4ec59bd2a2aabddd13d', 'encoding': None, 'history': [], 'reason': 'OK', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=135044), 'request': <PreparedRequest [PATCH]>, 'connect 16b77ead1718639c2ab727aa50c6a8a669a52da9 5ef9a4ec59bd2a2aabddd13d 263 { '_content':B'{ “_ ID”: “5ef9a4ec59bd2a2aabddd13d”, “_updated”: “周一,2020年10月5日9时22分26秒格林尼治标准时间”, “_created”:“星期一,2020年6月29日08:23 :08 GMT", "_etag": "fc0363f6e2b4c59a870c06e057b09c5b94dd44f5", "_links": {"self": {"title": "user", "href": "users/5ef9a4ec59abdd}","3a2OK_OK }', '_content_consumed': True, '_next': None, 'status_code': 200, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:26 GMT ', 'Content-Type': 'application/json', 'Content-Length': '276', 'Connection': 'keep-alive', 'ETag': '"fc0363f6e2b4c59a870c06e057b09c5b94dd44f5"-Allow', -Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8EA031788>, 'url': 'https://socialpark.iti.gr:8006/users/5ef9a4ec59bd2a2aabddd13d', 'encoding None, 'history': [], 'reason': 'OK', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=135044), 'request': <PreparedRequest [PATCH]> , '连接ion': <requests.adapters.HTTPAdapter object at 0x000001F8E8F06C48>} ion': <requests.adapters.HTTPAdapter 对象在 0x000001F8E8F06C48>}

ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 129 ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 129

{'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:28 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8E9FACC08>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=134461), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA484408>} {'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "客户端和服务器 etags 不匹配"}}', '_content_consumed': True , '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:28 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response. HTTPResponse 对象在 0x000001F8E9FACC08>,'url':'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742','encoding':无,'history':[],'PRECONDITION':'LED' 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=134461), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA484408>}

ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 136 ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 136

{'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:30 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8E9FAC048>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=96633), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA031788>} {'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "客户端和服务器 etags 不匹配"}}', '_content_consumed': True , '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:30 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response. HTTPResponse 对象在 0x000001F8E9FAC048>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'PRAINDITION': 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=96633), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA031788>}

16b77ead1718639c2ab727aa50c6a8a669a52da9 5ef9a4ec59bd2a2aabddd13d 284 16b77ead1718639c2ab727aa50c6a8a669a52da9 5ef9a4ec59bd2a2aabddd13d 284

{'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:32 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8E9FACCC8>, 'url': 'https://socialpark.iti.gr:8006/users/5ef9a4ec59bd2a2aabddd13d', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=89694), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA484188>} {'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "客户端和服务器 etags 不匹配"}}', '_content_consumed': True , '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:32 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response. HTTPResponse 对象在 0x000001F8E9FACCC8>, 'url': 'https://socialpark.iti.gr:8006/users/5ef9a4ec59bd2a2aabddd13d', 'encoding': None, 'history': [], 'reason': FAILED'PRECONDITION 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=89694), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA484188>}

ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 150 ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 150

{'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:34 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8EA031888>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=93464), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8E8E969C8>} {'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "客户端和服务器 etags 不匹配"}}', '_content_consumed': True , '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:34 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response. HTTPResponse 对象在 0x000001F8EA031888>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'PRAINDITION': 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=93464), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8E8E969C8>}

ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 157 ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 157

{'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:37 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8EA484908>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=152959), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA0311C8>} {'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "客户端和服务器 etags 不匹配"}}', '_content_consumed': True , '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:37 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response. HTTPResponse 对象在 0x000001F8EA484908>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'PRAINDITION': 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=152959), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA0311C8>}

16b77ead1718639c2ab727aa50c6a8a669a52da9 5ef9a4ec59bd2a2aabddd13d 305 16b77ead1718639c2ab727aa50c6a8a669a52da9 5ef9a4ec59bd2a2aabddd13d 305

{'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:39 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8EA0B9AC8>, 'url': 'https://socialpark.iti.gr:8006/users/5ef9a4ec59bd2a2aabddd13d', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=116027), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA2DBE88>} {'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "客户端和服务器 etags 不匹配"}}', '_content_consumed': True , '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:39 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response. HTTPResponse 对象在 0x000001F8EA0B9AC8>, 'url': 'https://socialpark.iti.gr:8006/users/5ef9a4ec59bd2a2aabddd13d', 'encoding': None, 'history': [], 'reason': FAILED'PRECO 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=116027), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter 对象在 0x000001F8EA2DBE88>}

Logs show that you do not have permission to update the users but only the first one.日志显示您无权更新用户,但只有第一个。 You should contact the backend developer and confirm what are the conditions for updating each user.您应该联系后端开发人员并确认更新每个用户的条件是什么。

It might be solved by using correct authorization header for each user.可以通过为每个用户使用正确的授权标头来解决。

More details on 412 Error.有关 412 错误的更多详细信息。 and this .这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM