使用三重询问的请求返回的响应与使用请求和aiohttp的响应不同

时间:2019-07-17 14:54:27

标签: python-3.x request python-requests http-post python-trio

好的,您好,所以我试图使用三重奏并询问(https://asks.readthedocs.io/)在我的Web应用程序中实现Opticalard(会员卡服务)。

所以我想向他们的查询API发送请求: 使用请求:

import requests
r = requests.post("https://merchant.opticard.com/public/do_inquiry.asp", data={'company_id':'Dt', 'FirstCardNum1':'foo', 'g-recaptcha-response':'foo','submit1':'Submit'})

这将返回“ Invalid ReCaptcha”,这是正常现象,也是我想要的

与aiohttp相同:

import asyncio
import aiohttp

async def fetch(session, url):
    async with session.post(url, data={'company_id':'Dt', 'FirstCardNum1':'foo', 'g-recaptcha-response':'foo','submit1':'Submit'} ) as response:
        return await response.text()

async def main():
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, 'https://merchant.opticard.com/public/do_inquiry.asp')
        print(html)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

现在,这还会返回“ Invalid ReCaptcha”,所以一切都很好。

但是现在,使用三重奏/问号:

import asks
import trio

async def example():
    r = await asks.post('https://merchant.opticard.com/public/do_inquiry.asp', data={'company_id':'Dt', 'FirstCardNum1':'foo', 'g-recaptcha-response':'foo','submit1':'Submit'})
    print(r.text)
trio.run(example)

这将返回完全不同的响应,其中“您的会话已过期以保护您的帐户。请重新登录。”,当输入无效的URL(例如“ https://merchant.opticard.com/do_inquiry.asp”而不是“ https://merchant.opticard.com/public/do_inquiry.asp”时,可以正常访问此错误/消息。

我不知道此错误来自何处,我尝试设置标头,Cookie,编码,似乎没有任何办法使它起作用。我尝试复制该问题,但是设法通过aiohttp和请求复制结果的唯一方法是设置一个错误的URL,例如“ https://merchant.opticard.com/do_inquiry.asp”而不是“ https://merchant.opticard.com/public/do_inquiry.asp”。

这一定是来自Asks的问题,可能是由于编码或格式,但是我使用Asks已有一年多了,从来没有遇到过这样的问题:简单的带有数据的发帖请求与其他地方的返回结果不同。而且我不知所措,因为我不明白为什么会这样,这在要求方面不可能是格式错误,因为如果是这样,为什么这是第一次使用超过一年?

1 个答案:

答案 0 :(得分:2)

这是一个错误,当收到非标准位置时,它会问如何处理重定向。

服务器返回带有Location: inquiry.asp?...的302重定向,而要求它是完整的URL。您可能要提交错误报告以询问。


我是怎么找到这个的?最好的方法是使用代理(例如mitmproxy)检查流量。但是问不支持代理。因此,我转而使用Wireshark,并使用a program提取TLS密钥,以便Wireshark可以解密流量。