Skip to content

Commit

Permalink
Add IP info to response
Browse files Browse the repository at this point in the history
  • Loading branch information
perklet committed Jun 26, 2024
1 parent de0dbd7 commit e3da5a3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions curl_cffi/requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Response:
elapsed: how many seconds the request cost.
encoding: http body encoding.
charset: alias for encoding.
primary_ip: primary ip of the server.
local_ip: local ip used in this connection.
charset_encoding: encoding specified by the Content-Type header.
default_encoding: encoding for decoding response content if charset is not found in
headers. Defaults to "utf-8". Can be set to a callable for automatic detection.
Expand All @@ -68,6 +70,8 @@ def __init__(self, curl: Optional[Curl] = None, request: Optional[Request] = Non
self.redirect_count = 0
self.redirect_url = ""
self.http_version = 0
self.primary_ip: str = ""
self.local_ip: str = ""
self.history: List[Dict[str, Any]] = []
self.infos: Dict[str, Any] = {}
self.queue: Optional[queue.Queue] = None
Expand Down
4 changes: 2 additions & 2 deletions curl_cffi/requests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,11 @@ def _parse_response(self, curl, buffer, header_buffer, default_encoding):
morsels = [CurlMorsel.from_curl_format(c) for c in c.getinfo(CurlInfo.COOKIELIST)]
# for l in c.getinfo(CurlInfo.COOKIELIST):
# print("Curl Cookies", l.decode())

self.cookies.update_cookies_from_curl(morsels)
rsp.cookies = self.cookies
# print("Cookies after extraction", self.cookies)

rsp.primary_ip = cast(bytes, c.getinfo(CurlInfo.PRIMARY_IP)).decode()
rsp.local_ip = cast(bytes, c.getinfo(CurlInfo.LOCAL_IP)).decode()
rsp.default_encoding = default_encoding
rsp.elapsed = cast(float, c.getinfo(CurlInfo.TOTAL_TIME))
rsp.redirect_count = cast(int, c.getinfo(CurlInfo.REDIRECT_COUNT))
Expand Down
9 changes: 9 additions & 0 deletions tests/unittest/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,3 +745,12 @@ def test_curl_infos(server):
r = s.get(str(server.url))

assert r.infos[CurlInfo.PRIMARY_IP] == b"127.0.0.1" # pyright: ignore


def test_response_ip(server):
s = requests.Session()
r = s.get(str(server.url))

assert r.primary_ip == "127.0.0.1"
assert r.local_ip == "127.0.0.1"

0 comments on commit e3da5a3

Please sign in to comment.