繁体   English   中英

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

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

我有一个代码,我想在其中为每个用户发出补丁请求(请参阅“for user in users”)。 循环内打印的输出是这样的:

5ee5eee85cb7558196e1f742

101

5ee5eee85cb7558196e1f742

108

5ef9a4ec59bd2a2aabddd13d

242

5ee5eee85cb7558196e1f742

122

5ee5eee85cb7558196e1f742

129

5ef9a4ec59bd2a2aabddd13d

263

5ee5eee85cb7558196e1f742

143

5ee5eee85cb7558196e1f742

150

5ef9a4ec59bd2a2aabddd13d

284

但是补丁只制作一次,并且只针对第一个“5ee5eee85cb7558196e1f742”和第一个“5ef9a4ec59bd2a2aabddd13d”,而不是循环中的所有其他时间。 有人可以帮助我吗?

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)

在这里你可以看到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/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": {"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”: “周一,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 对象在 0x000001F8E8F06C48>}

ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 129

{'_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

{'_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

{'_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

{'_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

{'_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

{'_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>}

日志显示您无权更新用户,但只有第一个。 您应该联系后端开发人员并确认更新每个用户的条件是什么。

可以通过为每个用户使用正确的授权标头来解决。

有关 412 错误的更多详细信息。 这个

暂无
暂无

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

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