簡體   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