Validates the proxy URL format. Raises: ValueError: If the proxy URL format is invalid or the port is out of range.
(self)
| 58 | await self._session.close() |
| 59 | |
| 60 | def _validate_proxy(self) -> None: |
| 61 | """Validates the proxy URL format. |
| 62 | |
| 63 | Raises: |
| 64 | ValueError: If the proxy URL format is invalid or the port is out of range. |
| 65 | """ |
| 66 | if not self.proxy: |
| 67 | return # Proxy is not set, which is acceptable |
| 68 | # Regular expression to check the format: socks5://user:pass@host:port, etc. |
| 69 | pattern = r'^(socks5|socks4|http|https)://(\w+:\w+@)?[\w.-]+:\d+$' |
| 70 | if not re.match(pattern, self.proxy): |
| 71 | raise ValueError( |
| 72 | f"Invalid proxy URL format: {self.proxy}. Expected format like 'socks5://user:pass@host:port'") |
| 73 | # Check the port |
| 74 | port = self.proxy.split(':')[-1] |
| 75 | if not port.isdigit() or not (1 <= int(port) <= 65535): |
| 76 | raise ValueError(f"Invalid port in proxy URL: {port}") |
| 77 | |
| 78 | async def _request(self, method: str, parameters: str, json_data=None) -> Optional[dict]: |
| 79 | if method not in self._SUPPORTED_METHODS: |