def __init__( self, endpoint_uri: Union[str, dict] = None, timeout: float = DEFAULT_TIMEOUT, client: httpx.AsyncClient = None, api_key: str = DEFAULT_API_KEY, ): super().__init__() if endpoint_uri is None: self.endpoint_uri = os.environ.get("TRONPY_HTTP_PROVIDER_URI", "https://api.trongrid.io/") elif isinstance(endpoint_uri, (dict, )): self.endpoint_uri = endpoint_uri["fullnode"] elif isinstance(endpoint_uri, (str, )): self.endpoint_uri = endpoint_uri else: raise TypeError("unknown endpoint uri {}".format(endpoint_uri)) headers = {"User-Agent": "Tronpy/0.2", "Tron-Pro-Api-Key": api_key} if client is None: self.client = httpx.AsyncClient(headers=headers, timeout=Timeout(timeout)) else: self.client = client self.timeout = timeout """Request timeout in second."""
def __init__( self, internal: bool = False, proxies: Optional[str] = None, headers: Optional[Dict[str, str]] = None, cookies: Optional[str] = None, ): self.internal: bool = internal if not headers: headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36" } else: headers.update(headers) self.cookies: Dict[str, str] = {} if cookies: for line in cookies.split(";"): key, value = line.strip().split("=", 1) self.cookies[key] = value transport = AsyncHTTPTransport(verify=False, retries=3) self.client: AsyncClient = AsyncClient( proxies=proxies, # type: ignore headers=headers, cookies=self.cookies, timeout=Timeout(20, connect=60), follow_redirects=True, transport=transport, )
def __init__(self): super().__init__('POST', 'https://postimages.org/json/rr', timeout=Timeout(5.0, read=10.0)) response = self.client.get('https://postimages.org') self.token = re.search('"token","(.+?)"', response.text)[1]
async def test_concurrent_read(server): """ Regression test for: https://github.com/encode/httpx/issues/527 """ backend = lookup_backend() stream = await backend.open_tcp_stream(server.url.host, server.url.port, ssl_context=None, timeout=Timeout(5)) timeout = Timeout(5) try: await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout) await run_concurrently(lambda: stream.read(10, timeout), lambda: stream.read(10, timeout)) finally: await stream.close()
async def main(): async with AsyncClient(base_url=Pages.main) as client: client._timeout = Timeout(3) SetSessionHeaders(client) t = time() with open("./PrivateConfig/mainPassword.txt") as f: password = f.readlines()[0] with open("./PrivateConfig/mail.ru-email.txt") as f: email = f.readlines()[0] if await Authorize(client, email, password): book: Book = Book(client) await book.GetBookFromUrl("work/40323") print(book.header) print("-----------------\nTable of Contents\n-----------------", end="\n") for chapterHeader in book.header.tableOfContents: print(chapterHeader) fb2 = FictionBook2() fb2.titleInfo.title = book.header.title fb2.titleInfo.authors = book.header.authors fb2.titleInfo.annotation = book.header.annotation fb2.titleInfo.genres = book.header.genres fb2.titleInfo.lang = "ru" fb2.titleInfo.sequences = [(book.header.sequence.name, book.header.sequence.number)] fb2.titleInfo.keywords = book.header.tags fb2.titleInfo.coverPageImages = [book.header.coverImageData] fb2.titleInfo.date = (book.header.publicationDate, None) fb2.chapters = list(map(lambda chapter: (chapter.header.title, chapter.paragraphs), await book.GetBookChapters())) fb2.write(f"./Output/{fb2.titleInfo.title}.fb2") await Logoff(client) print(f"All requests took {time() - t} seconds.")
async def test_start_tls_on_uds_socket_stream(https_uds_server): backend = lookup_backend() ctx = SSLConfig().load_ssl_context_no_verify() timeout = Timeout(5) stream = await backend.open_uds_stream(https_uds_server.config.uds, https_uds_server.url.host, None, timeout) try: assert stream.is_connection_dropped() is False assert get_cipher(stream) is None stream = await stream.start_tls(https_uds_server.url.host, ctx, timeout) assert stream.is_connection_dropped() is False assert get_cipher(stream) is not None await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout) response = await read_response(stream, timeout, should_contain=b"Hello, world") assert response.startswith(b"HTTP/1.1 200 OK\r\n") finally: await stream.close()
async def test_async_manual_client(): from httpx import AsyncClient, Timeout, Limits from tronpy.providers.async_http import AsyncHTTPProvider from tronpy.defaults import CONF_NILE _http_client = AsyncClient(limits=Limits(max_connections=100, max_keepalive_connections=20), timeout=Timeout(timeout=10, connect=5, read=5)) provider = AsyncHTTPProvider(CONF_NILE, client=_http_client) client = AsyncTron(provider=provider) priv_key = PrivateKey( bytes.fromhex( "8888888888888888888888888888888888888888888888888888888888888888") ) txb = (client.trx.transfer("TJzXt1sZautjqXnpjQT4xSCBHNSYgBkDr3", "TVjsyZ7fYF3qLF6BQgPmTEZy1xrNNyVAAA", 1_000).memo("test memo").fee_limit(1_000_000)) txn = await txb.build() txn_ret = await txn.sign(priv_key).broadcast() print(txn_ret) print(await txn_ret.wait()) # must call .close at end to release connections await client.close()
async def async_get(url: str, return_json: bool = True): async with httpx.AsyncClient(timeout=Timeout(timeout=10.0)) as client: raw_response = await client.get(url) if return_json: return raw_response.json() else: return raw_response
async def send_api_requests(endpoint: str, data: dict, nodes: List[APINode]): async with AsyncClient(timeout=Timeout(timeout=100.0)) as client: tasks = [ send_api_request(client, endpoint, data, node) for node in nodes ] for completed in asyncio.as_completed(tasks): res = await completed yield res
def http_post(endpoint_url, payload): headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} try: # Override default timeout to 10s with httpx.Client() as client: client.post(endpoint_url, data=json.dumps(payload), headers=headers, timeout=Timeout(timeout=10.0)) except httpx.ReadTimeout as e: print(f"httpx read timeout exception {e}") except urllib3.exceptions.ReadTimeoutError as e2: print(f"urllib3 read timeout exception {e2}")
async def test_not_modified_releases_connection(self, url): async_client = AsyncClient( timeout=Timeout(1, pool=0.1), limits=Limits(max_connections=1, max_keepalive_connections=1), ) async_client._transport = AsyncCachingTransport( transport=async_client._transport, ) # make sure the pool doesn't time out for _i in range(3): await async_client.get(url + "etag") await async_client.aclose()
def __init__(self, cookie_ffs, cookie_session): super().__init__('POST', 'https://up.flickr.com/services/upload', timeout=Timeout(5.0, read=60.0), cookies={ 'ffs': cookie_ffs, 'cookie_session': cookie_session }) self.last_response = self.client.get( 'https://www.flickr.com/photos/upload') auth_hash = re.search(r'"auth_hash":"([0-9a-f]{64})"', self.last_response.text) api_key = re.search(r'"api_key":"([0-9a-f]{32})"', self.last_response.text) user_id = re.search(r'"nsid":"([\w@]+)"', self.last_response.text) assert auth_hash and api_key and user_id, 'Incorrect Cookies info?' self.auth_hash, self.api_key, self.user_id = auth_hash[1], api_key[ 1], user_id[1]
async def init() -> None: """ Set properties for the app object :return: None """ app.data_queue = Queue() app.environment = os.environ.get('ENVIRONMENT', 'LOCAL') app.parquet_path = Path().cwd().joinpath('parquet') app.pickle_path = Path().cwd().joinpath('offset.pickle') app.logger = set_logging() app.api_session = AsyncClient( headers={ "Content-Type": "application/json", }, transport=AsyncHTTPTransport(retries=5), timeout=Timeout(connect=300, read=300, write=300, pool=5) ) app.current_offset = await get_current_offset() app.api_pagination_limit = 500 app.buffer = Buffer(mb_max_size=1)
def test_timeout(): # httpx will raise ConnectError in some conditions with raises((TimeoutException, ConnectError, ConnectTimeout)): translator = Translator(timeout=Timeout(0.0001)) translator.translate('안녕하세요.')
from aredis import StrictRedis from httpx import AsyncClient, Timeout from starlette.applications import Starlette from starlette.requests import Request from starlette.responses import JSONResponse, Response from starlette.routing import Route from config import debug, user_agent, redis_socket, trusted_ips from hnapi import HNClient from reader import Reader session = AsyncClient(timeout=Timeout(timeout=15.0), headers={ "User-Agent": user_agent, }) if redis_socket: r = StrictRedis(unix_socket_path=redis_socket) else: r = StrictRedis() reader = Reader() api = HNClient(session, r) async def item(request: Request): item_id = request.path_params["item_id"] data = await api.get_full_item(item_id) return JSONResponse(data) async def read(request: Request):
import asyncio from typing import Optional, Tuple from contextlib import asynccontextmanager from httpx import AsyncClient, Timeout import logging from starlette import status from json import JSONDecodeError import config from resources import strings LOGGER = logging.getLogger(__name__) TIMEOUT = Timeout(10, read=30) class InstallFailedException(Exception): pass def read_workspace_id() -> str: with open('workspace_id.txt', 'r') as f: workspace_id = f.readline() return workspace_id def write_workspace_id(workspace_id: str) -> None: with open('workspace_id.txt', 'w') as f: f.write(workspace_id) def get_auth_header(token: str) -> dict:
srt=in_lang, dest=out_lang) node.text = translated.text break except Exception as ex: print(ex) print("can't translate:", in_text) print("retrying...") time.sleep(5) if len(sys.argv) < 5: print( "args:\n* input xml filename\n* output xml filename\n* input language code \n* output language code\nlanguage codes:" ) print(LANGCODES) exit() in_filename = sys.argv[1] out_filename = sys.argv[2] in_lang = sys.argv[3] out_lang = sys.argv[4] translator = Translator(raise_exception=True, timeout=Timeout(10.0)) tree = ET.parse(in_filename) root = tree.getroot() translate_xml(root) tree.write(open(out_filename, "w", encoding="UTF-8"), encoding="unicode")
async def setup_api(): NeededVars.api_client = AsyncClient() timeout = Timeout(10.0, read=None) return timeout
#! /usr/bin/env python3 import asyncio from typing import Any, Dict, List, Optional import typer from httpx import URL, AsyncClient, Timeout, codes from pydantic import EmailStr, SecretStr DEFAULT_TIMEOUT = Timeout(30.0) async def login_user(client: AsyncClient, email: EmailStr, password: SecretStr): path = "/auth/login" r = await client.post( path, json={"email": email, "password": password.get_secret_value()} ) r.raise_for_status() async def get_project_for_user( client: AsyncClient, project_id: str ) -> Optional[Dict[str, Any]]: path = f"/projects/{project_id}" r = await client.get(path, params={"type": "user"}) if r.status_code == 200: response_dict = r.json() data = response_dict["data"] return data
async def request(method: Literal['GET', 'OPTIONS', 'HEAD', 'POST', 'PUT', 'PAtCH', 'DELETE'], url: HttpUrl, params: Optional[dict]=None, content: Optional[dict]=None, data: Optional[dict]=None, files: Optional[dict]=None, json: Optional[dict]=None, headers: Optional[dict]=None, cookies: Optional[dict]=None) -> Response: async with AsyncClient(timeout=Timeout(10.0, connect=60.0, read=60.0)) as client: return await client.request(method=method, url=url, params=params, content=content, data=data, files=files, json=json, headers=headers, cookies=cookies)