我有一个代码,我想在其中为每个用户发出补丁请求(请参阅"for user-in-users"中的)。循环内打印的输出是这样的:
5ee5ee85cb7558196e1f742
101
5ee5ee85cb7558196e1f742
108
242
5ee5ee85cb7558196e1f742
122
5ee5ee85cb7558196e1f742
129
263
5ee5ee85cb7558196e1f742
143
5ee5ee85cb7558196e1f742
150
284
但该补丁只制作了一次,而且只针对第一次"补丁";5ee5ee85cb7558196e1f742〃;而第一个";5ef9a4ec5bdd2a2aabddd13d";而不是针对循环中的所有其他时间。有人能帮我吗?
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'{quot;_id;:"5ee5ee85cb7558196e1f742","_updated":"Mon,05 Oct 2020 09:22:22 GMT","_created":"Sun,14 Jun 2020 09:33:27 GMT","_etag":"a97daa90626ed7493010f11d4be6ff06e6a7be","_litag"nks":{"self":{"title":"user","href&":"users/5eeee85cb7558196e1f742"},"_状态":"OK"}','_content_consumed':True,'_next':None,'status_code':200,'headers':{'Server':'nginx','Date':'Mon,05 Oct 2020 09:22:22GMT','ContentType':'application/json','contentLength':'276','Connection':'keep-alive','ETag':'"a97daa90626ed7493010f11d4be6ff06e6a7be"','Access Control Allow Origin':'*'},'raw':<位于0x000001F8E9F68D08>的urlib3.response.HTTPResponse对象,'url":"https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742','encoding':无,'history':[],'reason':'OK','cookies':<RequestsCookieJar[]>,'逝去':datetime.timedelta(微秒=9716),'请求':<PreparedRequest[PACH]>,'连接':<0x000001F8EA297F88>}处的requests.adapters.HTTPdapter对象
ab295b633d9c4b727a3c0342294fa463d5cfe6a25ee5ee85cb7558196e1f742115{'_content':b'{quot;_status":"ERR","_error":{quotquot;code":412,"message":"Client and server etags not match"}}','_content_sumed':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":<0x000001F8E9FAC6C8>处的urlib3.response.HTTPResponse对象,'url":"https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742','encoding':无,'history':[],'reason':'PRECONDITION FAILED','cookies':<RequestsCookieJar[]>,'逝去':datetime.timedelta(微秒=102812),'请求':<PreparedRequest[PACH]>,'连接':<0x000001F8EA172088>}处的requests.adapters.HTTPdapter对象
16b77头1718639c2ab727aa50c6a8a669a52da9第5页263{'_content':b'{quot;_id":"5ef9a4ec59bd2aaabddd13d","_updated":"2020年10月5日星期一09:22:26 GMT","_created":"2020年6月29日星期一08:23:08 GMT","_etag":"fc0363f6e2b4c59a870c06e057b09c5b94dd44f5","_litag nks":{"self":{"title":"user","href&":"users/5ef9a4ec5bd2a2abddd13d"},"_状态":"OK"}','_content_consumed':True,'_next':None,'status_code':200,'headers':{'Server':'nginx','Date':'Mon,05 Oct 2020 09:22:26GMT','content Type':'application/json','content-Length':'276','Connection':'keep-alive','ETag':'"fc0363f6e2b4c59a870c06e057b09c5b94dd44f5"','Access Control Allow Origin':'*'},'raw':<位于0x000001F8EA031788>的urllib3.response.HTTPResponse对象,'url":"https://socialpark.iti.gr:8006/users/5ef9a4ec59bd2a2aabddd13d','encoding':无,'history':[],'reason':'OK','cookies':<RequestsCookieJar[]>,'逝去':datetime.timedelta(微秒=135044),'请求':<PreparedRequest[PACH]>,'连接':<0x000001F8E8F06C48>}处的requests.adapters.HTTPdapter对象
ab295b633d9c4b727a3c0342294fa463d5cfe6a25ee5ee85cb7558196e1f742129
{'_content':b'{quot;_status":"ERR","_error":{quotquot;code":412,"message":"Client and server etags not match"}}','_content_sumed':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":<0x000001F8E9FACC08>处的urlib3.response.HTTPResponse对象,'url":"https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742','encoding':无,'history':[],'reason':'PRECONDITION FAILED','cookies':<RequestsCookieJar[]>,'逝去':datetime.timedelta(微秒=134461),'请求':<PreparedRequest[PACH]>,'连接':<requests.adapters.HTTPdapter对象位于0x000001F8EA484408>}
ab295b633d9c4b727a3c0342294fa463d5cfe6a25ee5ee85cb7558196e1f742136
{'_content':b'{quot;_status":"ERR","_error":{quotquot;code":412,"message":"Client and server etags not match"}}','_content_sumed':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":<位于0x000001F8E9FAC048>的urllib3.response.HTTPResponse对象,'url":"https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742','encoding':无,'history':[],'reason':'PRECONDITION FAILED','cookies':<RequestsCookieJar[]>,'逝去的':datetime.timedelta(微秒=96633),'请求':<PreparedRequest[PACH]>,'连接':<requests.adapters.HTTPAdapter对象位于0x000001F8EA031788>}
16b77头1718639c2ab727aa50c6a8a669a52da9第5页284
{'_content':b'{quot;_status":"ERR","_error":{quotquot;code":412,"message":"Client and server etags not match"}}','_content_sumed':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":<位于0x000001F8E9FACCC8>的urlib3.response.HTTPResponse对象,'url":"https://socialpark.iti.gr:8006/users/5ef9a4ec59bd2a2aabddd13d','encoding':无,'history':[],'reason':'PRECONDITION FAILED','cookies':<RequestsCookieJar[]>,'逝去':datetime.timedelta(微秒=89694),'请求':<PreparedRequest[PACH]>,'连接':<requests.adapters.HTTPAdapter对象位于0x000001F8EA484188>}
ab295b633d9c4b727a3c0342294fa463d5cfe6a25ee5ee85cb7558196e1f742150
{'_content':b'{quot;_status":"ERR","_error":{quotquot;code":412,"message":"Client and server etags not match"}}','_content_sumed':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":<位于0x000001F8EA031888的urllib3.response.HTTPResponse对象>,'url":"https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742','encoding':无,'history':[],'reason':'PRECONDITION FAILED','cookies':<RequestsCookieJar[]>,'逝去的':datetime.timedelta(微秒=93464),'请求':<PreparedRequest[PACH]>,'连接':<0x000001F8E8E969C8>}处的requests.adapters.HTTPdapter对象
ab295b633d9c4b727a3c0342294fa463d5cfe6a25ee5ee85cb7558196e1f742157
{'_content':b'{quot;_status":"ERR","_error":{quotquot;code":412,"message":"Client and server etags not match"}}','_content_sumed':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":<位于0x000001F8EA484908>的urlib3.response.HTTPResponse对象,'url":"https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742','encoding':无,'history':[],'reason':'PRECONDITION FAILED','cookies':<RequestsCookieJar[]>,'逝去':datetime.timedelta(微秒=152959),'请求':<PreparedRequest[PACH]>,'连接':<0x000001F8EA0311C8>}处的requests.adapter.HTTPAdapter对象
16b77头1718639c2ab727aa50c6a8a669a52da9第5页305
{'_content':b'{quot;_status":"ERR","_error":{quotquot;code":412,"message":"Client and server etags not match"}}','_content_sumed':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":<位于0x000001F8EA0B9AC8>的urlib3.response.HTTPResponse对象,'url":"https://socialpark.iti.gr:8006/users/5ef9a4ec59bd2a2aabddd13d','encoding':无,'history':[],'reason':'PRECONDITION FAILED','cookies':<RequestsCookieJar[]>,'逝去的':datetime.timedelta(微秒=116027),'请求':<PreparedRequest[PACH]>,'连接':<0x000001F8EA2DBE88>}处的requests.adapter.HTTPAdapter对象
日志显示您无权更新用户,只能更新第一个用户。您应该联系后端开发人员,确认更新每个用户的条件是什么。
它可以通过为每个用户使用正确的授权头来解决。
有关412错误的更多详细信息。还有这个。