代码之家  ›  专栏  ›  技术社区  ›  PhD Gangstead

为什么Python请求在循环中发出POST时返回相同的数据,即使在关闭会话之后也是如此?

  •  0
  • PhD Gangstead  · 技术社区  · 8 年前

    我在用Python Requests 但需要向SOAP API端点发出请求/发布。然而,尽管我在循环中重新生成了一个新的请求,我仍然会对多个请求接收相同的响应。我尝试过结束会议/回复,但没有效果。

    手动发出请求会返回不同的数据。我会错过什么?

    下面是代码示例:

    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 = ""
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   PhD Gangstead    8 年前

    令人惊讶的是,将此代码移动到一个单独的 班 似乎解决了这个问题——我的猜测是,全局/状态似乎摆脱了python请求模块(我可能错了)。

    另外,搬到一个班级 with 块成为可选的,也就是说,它是否存在似乎并不重要-代码在有/没有它的情况下按预期工作。

    例如:

    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
    

    我只能猜测globals在构建/发送请求和/或接收响应时会以某种方式引发问题。