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

View file

@ -15,19 +15,27 @@ class Proxy:
def __init__(self,
proxy_type: str='http',
host: str='127.0.0.1',
host_or_ip: str='127.0.0.1',
port: int=8080,
username: str=None,
password: str=None
):
self.proxy_type = proxy_type
self.ip_address = host
self.host = socket.gethostbyname(host)
self.host_or_ip = host_or_ip
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.username = username
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 with aiohttp.ClientSession(
@ -54,7 +62,7 @@ class Proxy:
connector = aiohttp_socks.ProxyConnector(
proxy_type=proxy_types[self.proxy_type],
host=self.ip_address,
host=self.host,
port=self.port,
rdns=False,
username=self.username,
@ -67,23 +75,32 @@ class Proxy:
default_proxy = Proxy(
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')),
username=os.getenv('PROXY_USER'),
password=os.getenv('PROXY_PASS')
)
if __name__ == '__main__':
def test_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',
timeout=5,
proxies={
'https': default_proxy.url
}
def test_httpx():
import httpx
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()
print(received_ip)
if __name__ == '__main__':
print(test_requests())
print(test_httpx())