Skip to content

added AsyncSession #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,45 @@ res = session.post(
)
```

Example 3 - Async Preset:
```python
import asyncio
import tls_client

# You can also use the following as `client_identifier`:
# Chrome --> chrome_103, chrome_104, chrome_105, chrome_106, chrome_107, chrome_108, chrome109, Chrome110,
# chrome111, chrome112
# Firefox --> firefox_102, firefox_104, firefox108, Firefox110
# Opera --> opera_89, opera_90
# Safari --> safari_15_3, safari_15_6_1, safari_16_0
# iOS --> safari_ios_15_5, safari_ios_15_6, safari_ios_16_0
# iPadOS --> safari_ios_15_6
# Android --> okhttp4_android_7, okhttp4_android_8, okhttp4_android_9, okhttp4_android_10, okhttp4_android_11,
# okhttp4_android_12, okhttp4_android_13

session = tls_client.AsyncSession(
client_identifier="chrome112",
random_tls_extension_order=True
)


async def send_request():
response = await session.post(
"https://tls.peet.ws/api/tls",
headers={
"key1": "value1",
},
json={
"key1": "key2"
}
)
print(response.text)

loop = asyncio.get_event_loop()
loop.run_until_complete(send_request())
```


# Pyinstaller / Pyarmor
**If you want to pack the library with Pyinstaller or Pyarmor, make sure to add this to your command:**

Expand Down
35 changes: 35 additions & 0 deletions examples/example3 - async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import asyncio
import tls_client

# You can also use the following as `client_identifier`:
# Chrome --> chrome_103, chrome_104, chrome_105, chrome_106, chrome_107, chrome_108, chrome109, Chrome110,
# chrome111, chrome112
# Firefox --> firefox_102, firefox_104, firefox108, Firefox110
# Opera --> opera_89, opera_90
# Safari --> safari_15_3, safari_15_6_1, safari_16_0
# iOS --> safari_ios_15_5, safari_ios_15_6, safari_ios_16_0
# iPadOS --> safari_ios_15_6
# Android --> okhttp4_android_7, okhttp4_android_8, okhttp4_android_9, okhttp4_android_10, okhttp4_android_11,
# okhttp4_android_12, okhttp4_android_13

session = tls_client.AsyncSession(
client_identifier="chrome112",
random_tls_extension_order=True
)


async def send_request():
response = await session.post(
"https://tls.peet.ws/api/tls",
headers={
"key1": "value1",
},
json={
"key1": "key2"
}
)
print(response.text)

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

3 changes: 2 additions & 1 deletion tls_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
# tls-client: https://github.com/bogdanfinn/tls-client
# requests: https://github.com/psf/requests

from .sessions import Session
from .sessions import Session
from .async_client import AsyncSession
1 change: 1 addition & 0 deletions tls_client/async_client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .sessions import AsyncSession
49 changes: 49 additions & 0 deletions tls_client/async_client/sessions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import asyncio
from asyncio import Future
from typing import Optional, Union, Any
from tls_client import Session
from tls_client.response import Response


class AsyncSession(Session):

def execute_request(
self,
method: str,
url: str,
params: Optional[dict] = None, # Optional[dict[str, str]]
data: Optional[Union[str, dict]] = None,
headers: Optional[dict] = None, # Optional[dict[str, str]]
cookies: Optional[dict] = None, # Optional[dict[str, str]]
json: Optional[dict] = None, # Optional[dict]
allow_redirects: Optional[bool] = False,
insecure_skip_verify: Optional[bool] = False,
timeout_seconds: Optional[int] = None,
proxy: Optional[dict] = None # Optional[dict[str, str]]
) -> Future[Response]:
loop = asyncio.get_running_loop()
return loop.run_in_executor(None, super().execute_request, method, url, params, data, headers, cookies,
json, allow_redirects, insecure_skip_verify, timeout_seconds, proxy)

def get(self, url: str, **kwargs: Any) -> Future[Response]:
return super().get(url, **kwargs)

def options(self, url: str, **kwargs: Any) -> Future[Response]:
return super().options(url, **kwargs)

def head(self, url: str, **kwargs: Any) -> Future[Response]:
return super().head(url, **kwargs)

def post(self, url: str, data: Optional[Union[str, dict]] = None, json: Optional[dict] = None, **kwargs: Any) -> Future[Response]:
return super().post(url, data, json, **kwargs)

def put(self, url: str, data: Optional[Union[str, dict]] = None, json: Optional[dict] = None, **kwargs: Any) -> Future[Response]:
return super().put(url, data, json, **kwargs)

def patch(self, url: str, data: Optional[Union[str, dict]] = None, json: Optional[dict] = None, **kwargs: Any) -> Future[Response]:
return super().patch(url, data, json, **kwargs)

def delete(self, url: str, **kwargs: Any) -> Future[Response]:
return super().delete(url, **kwargs)