why does httpx have no rnds support >:(

This commit is contained in:
nsde 2023-07-27 03:44:53 +02:00
parent 08a8755201
commit 74f70130c4
2 changed files with 50 additions and 30 deletions

View file

@ -1,5 +1,5 @@
import os import os
import aiohttp import httpx
import proxies import proxies
@ -9,21 +9,24 @@ from request_manager import Request
load_dotenv() load_dotenv()
async def stream_closedai_request(request: Request): async def stream_closedai_request(request: Request):
async with aiohttp.ClientSession( async with httpx.AsyncClient(
connector=await proxies.default_proxy.get_connector(), # proxies=proxies.default_proxy.urls_httpx,
timeout=aiohttp.ClientTimeout(total=request.timeout), timeout=httpx.Timeout(request.timeout)
raise_for_status=False ) as client:
) as session:
async with session.request(
method=request.method,
url=request.url,
json=request.payload,
headers = { headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': f'Bearer {os.getenv("CLOSEDAI_KEY")}' 'Authorization': f'Bearer {os.getenv("CLOSEDAI_KEY")}'
} }
) as response: response = await client.request(
async for chunk in response.content.iter_any(): method=request.method,
url=request.url,
json=request.payload,
headers=headers
)
response.raise_for_status()
async for chunk in response.aiter_bytes():
chunk = f'{chunk.decode("utf8")}\n\n' chunk = f'{chunk.decode("utf8")}\n\n'
yield chunk yield chunk

View file

@ -15,19 +15,27 @@ class Proxy:
def __init__(self, def __init__(self,
proxy_type: str='http', proxy_type: str='http',
host: str='127.0.0.1', host_or_ip: str='127.0.0.1',
port: int=8080, port: int=8080,
username: str=None, username: str=None,
password: str=None password: str=None
): ):
self.proxy_type = proxy_type self.proxy_type = proxy_type
self.ip_address = host self.host_or_ip = host_or_ip
self.host = socket.gethostbyname(host) self.ip_address = socket.gethostbyname(self.host_or_ip) if host_or_ip[0].isdigit() else host_or_ip
self.host = self.host_or_ip
self.port = port self.port = port
self.username = username self.username = username
self.password = password self.password = password
self.url = f'socks5://{self.username}:{self.password}@{self.ip_address}:{self.port}' self.url = f'{self.proxy_type}://{self.username}:{self.password}@{self.host}:{self.port}'
self.urls = {
'http': self.url,
'https': self.url
}
self.urls_httpx = {k + '://' :v for k, v in self.urls.items()}
self.proxies = self.url
async def initialize_connector(self, connector): async def initialize_connector(self, connector):
async with aiohttp.ClientSession( async with aiohttp.ClientSession(
@ -54,7 +62,7 @@ class Proxy:
connector = aiohttp_socks.ProxyConnector( connector = aiohttp_socks.ProxyConnector(
proxy_type=proxy_types[self.proxy_type], proxy_type=proxy_types[self.proxy_type],
host=self.ip_address, host=self.host,
port=self.port, port=self.port,
rdns=False, rdns=False,
username=self.username, username=self.username,
@ -67,23 +75,32 @@ class Proxy:
default_proxy = Proxy( default_proxy = Proxy(
proxy_type=os.getenv('PROXY_TYPE', 'http'), proxy_type=os.getenv('PROXY_TYPE', 'http'),
host=os.getenv('PROXY_HOST', '127.0.0.1'), host_or_ip=os.getenv('PROXY_HOST', '127.0.0.1'),
port=int(os.getenv('PROXY_PORT', '8080')), port=int(os.getenv('PROXY_PORT', '8080')),
username=os.getenv('PROXY_USER'), username=os.getenv('PROXY_USER'),
password=os.getenv('PROXY_PASS') password=os.getenv('PROXY_PASS')
) )
if __name__ == '__main__': def test_requests():
import requests import requests
print(default_proxy.url) # return requests.get(
# 'https://checkip.amazonaws.com',
# timeout=5,
# proxies=default_proxy.urls
# ).text.strip()
received_ip = requests.get(
'https://checkip.amazonaws.com', def test_httpx():
timeout=5, import httpx
proxies={
'https': default_proxy.url print(default_proxy.proxies)
}
with httpx.Client(proxies=default_proxy.proxies, headers={'Host': 'checkip.amazonaws.com'}) as client:
return client.get(
f'http://{socket.gethostbyname("checkip.amazonaws.com")}/',
).text.strip() ).text.strip()
print(received_ip) if __name__ == '__main__':
print(test_requests())
print(test_httpx())