Returns the IP address of the current Tor client the service is using.
(client: httpx.Client)
| 13 | |
| 14 | |
| 15 | def get_ip(client: httpx.Client) -> dict: |
| 16 | """ |
| 17 | Returns the IP address of the current Tor client the service is using. |
| 18 | """ |
| 19 | resp = client.get("https://check.torproject.org/") |
| 20 | soup = BeautifulSoup(resp.text, 'html.parser') |
| 21 | |
| 22 | # Get the content of check tor project, this contains the header and body |
| 23 | content = soup.find("div", {"class": "content"}) |
| 24 | if not content: |
| 25 | raise Exception("unable to find content to parse IP.") |
| 26 | |
| 27 | # parse the header |
| 28 | header_tag = content.find("h1") |
| 29 | if not header_tag: |
| 30 | raise Exception("unable to find header") |
| 31 | if not isinstance(header_tag, Tag): |
| 32 | raise Exception("invalid header found") |
| 33 | header = header_tag.get_text().strip() |
| 34 | |
| 35 | # parse the main content containing the IP address |
| 36 | body_tag = content.find("p") |
| 37 | if not body_tag: |
| 38 | raise Exception("unable to find body") |
| 39 | if not isinstance(body_tag, Tag): |
| 40 | raise Exception("invalid body found") |
| 41 | body = body_tag.get_text().strip() |
| 42 | |
| 43 | return {"header": header, "body": body} |
no outgoing calls
no test coverage detected