有没有一种方法可以将HTTP动词(PATCH / POST)传递给函数并在Python请求中动态使用该动词?
例如,我希望这个函数接受一个'verb'变量,只在内部调用,并且将等于post/patch。
def dnsChange(self, zID, verb):
for record in config.NEW_DNS:
### LINE BELOW IS ALL THAT MATTERS TO THIS QUESTION
json = requests.verb(headers=self.auth, url=self.API + '/zones/' + str(zID) + '/dns_records', data={"type":record[0], "name":record[1], "content":record[2]})
key = record[0] + "record with host " + record[1]
result = json.loads(json.text)
self.apiSuccess(result,key,value)
我意识到我不能像上面那样请求'verb',那只是为了阐明问题。有没有办法做到这一点或类似的事情?我想避免一个:
I 理解我不能像上述那样请求“verb”,这只是为了解释问题。是否有方法实现此功能或类似功能?我希望避免出现:if verb == 'post':
json = requests.post(headers=self.auth, url=self.API + '/zones/' + str(zID) + '/dns_records', data={"type":record[0], "name":record[1], "content":record[2]}
else:
json = requests.patch(headers=self.auth, url=self.API + '/zones/' + str(zID) + '/dns_records', data={"type":record[0], "name":record[1], "content":record[2]}
谢谢大家!
getattr()
通常被认为是不好的。我建议你看看我的解决方案,因为它更好,这是requests
库应该使用的方式。 - Guillaumerequest
)的库进行工作的答案。我很好奇为什么你要等一年多才提出这个问题。 - metatoaster