jawah / niquests

“Safest, Fastest, Easiest, and Most advanced” Python HTTP Client. Production Ready! Drop-in replacement for Requests. HTTP/1.1, HTTP/2, and HTTP/3 supported. With WebSocket!

Home Page:https://niquests.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UnavailableTraffic: Cannot memorize traffic indicator upon unknown connection when using AsyncSession

JackTheMico opened this issue · comments

Met UnavailableTraffic: Cannot memorize traffic indicator upon unknown connection error when using AsyncSession post method to request data.

I met the problem when I was trying to replace aiohttp with niquests.I also tried the multiplexed=True, set proxies and tried the same code on MacOS. Still met the same error.

Reproduction Steps

import asyncio
import ujson as json
from loguru import logger
from niquests import AsyncSession, AsyncResponse

from copy import deepcopy


async def anker_comments_fetch(url: str, payload):
    # page = payload["methods"][0]["params"]["page"]
    # logger.debug(f"Anker fetching comments page: {page}")
    # logger.debug(f"Anker comments payload: {payload}")
    with AsyncSession() as session:
        logger.debug(url)
        # proxies = {
        #     "http": "http://172.27.176.1:7890",
        #     "https": "http://172.27.176.1:7890",
        # }
        response: AsyncResponse = await session.post(url, json=payload)
        return response
        # raw = await response.text()
        # res_json = json.loads(raw)
        # return res_json[0]["result"]


async def main():
    payload = {
        "methods": [
            {
                "method": "reviews",
                "params": {
                    "pid": "7180620791958",
                    "order_metadata_fields": {},
                    "widget_product_id": "7180620791958",
                    "data_source": "default",
                    "page": 1,
                    "host-widget": "main_widget",
                    "is_mobile": False,
                    "pictures_per_review": 10,
                },
            }
        ],
        "app_key": "zrJExX9I0FNoUytGcDfqDndBOu8OAdlKlrzm1Glz",
        "is_mobile": False,
    }
    payloads = []
    for page in range(1, 11):
        copypl = deepcopy(payload)
        copypl["methods"][0]["params"]["page"] = page
        payloads.append(copypl)
    url = "https://staticw2.yotpo.com/batch/app_key/zrJExX9I0FNoUytGcDfqDndBOu8OAdlKlrzm1Glz/domain_key/7180620791958/widget/reviews"
    tasks = []
    for payload in payloads:
        tasks.append(anker_comments_fetch(url, payload))
    results = await asyncio.gather(*tasks)
    print(results)


if __name__ == "__main__":
    asyncio.run(main())

System Information

$ python -m niquests.help
{
  "charset_normalizer": {
    "version": "3.3.2"
  },
  "cryptography": {
    "version": "42.0.3"
  },
  "http1": {
    "h11": "0.14.0"
  },
  "http2": {
    "h2": "4.1.0"
  },
  "http3": {
    "enabled": true,
    "qh3": "0.15.0"
  },
  "idna": {
    "version": "3.6"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.9.18"
  },
  "niquests": {
    "version": "3.4.5"
  },
  "ocsp": {
    "enabled": true
  },
  "platform": {
    "release": "5.15.133.1-microsoft-standard-WSL2",
    "system": "Linux"
  },
  "system_ssl": {
    "version": "30200000"
  },
  "urllib3.future": {
    "version": "2.5.903"
  },
  "wassima": {
    "certifi_fallback": false,
    "enabled": true,
    "version": "1.1.0"
  }
}

Great to see your test.

A few remarks,

  • Change with AsyncSession() as session: with async with AsyncSession() as session: instead.
  • Without stream=True, response: AsyncResponse = await session.post(url, json=payload) wont produce a "AsyncResponse" but rather a "Response", because it will already be fully loaded at return.

I just tried your code sample with said adjustments and latest versions and couldn't reproduce the error.
Can you try to update both pip install niquests urllib3.future -U and see if the issue persist?

If its okay, feel free to close this issue. Keep us posted if anything happen.
Lastly, if your code sample contain sensitive intels, take the necessary steps forward please.

Regards,

Thank you @Ousret , everything works fine after your adjustments.
I'll close this one.