Python 2.7 为什么Python请求在循环中发出POST时返回相同的数据,即使在关闭会话之后也是如此?
我正在使用Python,但需要向SOAP API端点发出请求/POST。但是,即使我在循环中重新生成一个新请求,我仍然会对多个请求接收相同的响应。我尝试过结束会议/回复,但没有效果 手动发出请求会返回不同的数据。我会错过什么 下面是代码示例:Python 2.7 为什么Python请求在循环中发出POST时返回相同的数据,即使在关闭会话之后也是如此?,python-2.7,http,python-requests,Python 2.7,Http,Python Requests,我正在使用Python,但需要向SOAP API端点发出请求/POST。但是,即使我在循环中重新生成一个新请求,我仍然会对多个请求接收相同的响应。我尝试过结束会议/回复,但没有效果 手动发出请求会返回不同的数据。我会错过什么 下面是代码示例: quoteVariables = """ <QuoteVariables> {0} </QuoteVariables>""" for state, zipcode in states.iteritems(): for k
quoteVariables = """
<QuoteVariables>
{0}
</QuoteVariables>"""
for state, zipcode in states.iteritems():
for key, value in buckets.iteritems():
quotesXMLWithStateZip = buildQuote() #returns long xml string
quotes = quoteVariables.format(quotesXMLWithStateZip)
soapRequest=soapXML.format(state, quotes, value)
headers = {'content-Type':'text/xml;charset=UTF-8', 'SOAPAction':'http://my.url.com', 'Connection':'close'}
with requests.Session() as s:
response = s.post('https://my.url.com/endpoint', headers=headers, data=soapRequest, stream=False)
if response.status_code == 200:
xml = xmltodict.parse(response.text)
#fetch relevant part of xml resposne, ignore soap headers etc.,
else:
print "Failure! Status: "+response.status_code+" Reason: "+response.reason
response.close()
s.close()
#reset xml stuff to prevent stale data from lying around owing to string sharing/copying
quotesXMLWithStateZip = ""
quotes = ""
soapRequest = ""
quoteVariables=“”
{0}
"""
对于state,在states.iteritems()中使用zipcode:
对于键,以bucket为单位的值。iteritems():
quotesXMLWithStateZip=buildQuote()#返回长xml字符串
quotes=QUOTEVARIABES.format(quotesXMLWithStateZip)
soapRequest=soapXML.format(状态、引号、值)
headers={'content-Type':'text/xml;charset=UTF-8','SOAPAction':'http://my.url.com“,”连接“:”关闭“}
将requests.Session()作为s:
响应=s.post('https://my.url.com/endpoint,headers=headers,data=soapRequest,stream=False)
如果response.status_code==200:
xml=xmltodict.parse(response.text)
#获取xml响应的相关部分,忽略soap头等。,
其他:
打印“失败!状态:+响应.状态代码+”原因:+响应.原因
答复:close()
s、 关闭()
#重置xml stuff以防止由于字符串共享/复制而导致过时数据四处散播
quotesXMLWithStateZip=“”
quotes=“”
soapRequest=“”
令人惊讶的是,将此代码移动到一个单独的类中似乎解决了问题-我的猜测是,全局/状态的某些东西似乎脱离了python请求模块(我可能错了)
此外,移动到类时,带的块变为可选块,即它是否存在似乎无关紧要-带/不带它的代码按预期工作
例如:
responses = {}
for state, zipcode in states.iteritems():
for key, value in buckets.iteritems():
fetcher = MyFetcher()
responses[state] = fetcher.getQuotes(state, zipcode, key, value)
print responses
MyFetcher
的代码类似于:
class MyFetcher:
def getQuotes(self, state, zipcode, key, value):
quotesXMLWithStateZip = self.buildQuote() #returns long xml string
quotes = self.quoteVariables.format(quotesXMLWithStateZip)
soapRequest=self.soapXML.format(state, quotes, value)
headers = {'content-Type':'text/xml;charset=UTF-8', 'SOAPAction':'http://my.url.com', 'Connection':'close'}
with requests.Session() as s: # <- ENTIRELY OPTIONAL
response = s.post('https://my.url.com/endpoint', headers=headers, data=soapRequest, stream=False)
if response.status_code == 200:
xml = xmltodict.parse(response.text)
#fetch relevant part of xml resposne, ignore soap headers etc.,
return parsedResponse
else:
print "Failure! Status: "+response.status_code+" Reason: "+response.reason
return None
response.close() # <- NOT NEEDED
s.close() # <- NOT NEEDED
类MyFetcher:
def getQuotes(self、state、zipcode、key、value):
quotesXMLWithStateZip=self.buildQuote()#返回长xml字符串
quotes=self.QUOTEVARIABES.format(quotesXMLWithStateZip)
soapRequest=self.soapXML.format(状态、引号、值)
headers={'content-Type':'text/xml;charset=UTF-8','SOAPAction':'http://my.url.com“,”连接“:”关闭“}
将requests.Session()作为s:#