Requests HTTP resource :param str method: method that should be issued e.g. GET, POST :param str path: path to the resource that should be requested :param requests session: session manager that should be used :param kwargs: kwargs passed to request method :
(self, method: str, path: str, session: requests = requests, **kwargs)
| 23 | ssl = OptBool(False, "SSL enabled: true/false") |
| 24 | |
| 25 | def http_request(self, method: str, path: str, session: requests = requests, **kwargs) -> requests.Response: |
| 26 | """ Requests HTTP resource |
| 27 | |
| 28 | :param str method: method that should be issued e.g. GET, POST |
| 29 | :param str path: path to the resource that should be requested |
| 30 | :param requests session: session manager that should be used |
| 31 | :param kwargs: kwargs passed to request method |
| 32 | :return Response: Response object |
| 33 | """ |
| 34 | |
| 35 | if self.ssl: |
| 36 | url = "https://" |
| 37 | else: |
| 38 | url = "http://" |
| 39 | |
| 40 | url += "{}:{}{}".format(self.target, self.port, path) |
| 41 | |
| 42 | kwargs.setdefault("timeout", HTTP_TIMEOUT) |
| 43 | kwargs.setdefault("verify", False) |
| 44 | kwargs.setdefault("allow_redirects", False) |
| 45 | |
| 46 | try: |
| 47 | return getattr(session, method.lower())(url, **kwargs) |
| 48 | except (requests.exceptions.MissingSchema, requests.exceptions.InvalidSchema): |
| 49 | print_error("Invalid URL format: {}".format(url), verbose=self.verbosity) |
| 50 | except requests.exceptions.ConnectionError: |
| 51 | print_error("Connection error: {}".format(url), verbose=self.verbosity) |
| 52 | except requests.RequestException as error: |
| 53 | print_error(error, verbose=self.verbosity) |
| 54 | except socket.error as err: |
| 55 | print_error(err, verbose=self.verbosity) |
| 56 | except KeyboardInterrupt: |
| 57 | print_error("Module has been stopped", verbose=self.verbosity) |
| 58 | |
| 59 | return None |
| 60 | |
| 61 | def get_target_url(self, path: str = "") -> str: |
| 62 | """ Get target URL |