Perform a HTTP(s) request. :param url: the full URL to connect to. e.g. https://google.com/test :param data: the data to send as payload :param follow_redirects: if True, request() will follow 302 return codes :param http_headers: if specified, o
(
self,
url,
data=b"",
timeout=5,
follow_redirects=True,
http_headers={},
**headers,
)
| 851 | return resp |
| 852 | |
| 853 | def request( |
| 854 | self, |
| 855 | url, |
| 856 | data=b"", |
| 857 | timeout=5, |
| 858 | follow_redirects=True, |
| 859 | http_headers={}, |
| 860 | **headers, |
| 861 | ): |
| 862 | """ |
| 863 | Perform a HTTP(s) request. |
| 864 | |
| 865 | :param url: the full URL to connect to. |
| 866 | e.g. https://google.com/test |
| 867 | :param data: the data to send as payload |
| 868 | :param follow_redirects: if True, request() will follow 302 return codes |
| 869 | :param http_headers: if specified, overwrites the HTTP headers |
| 870 | (except Host and Path). |
| 871 | :param headers: any additional HTTPRequest parameter to add. |
| 872 | e.g. Method="POST" |
| 873 | """ |
| 874 | # Parse request url |
| 875 | m = re.match(r"(https?)://([^/:]+)(?:\:(\d+))?(/.*)?", url) |
| 876 | if not m: |
| 877 | raise ValueError("Bad URL !") |
| 878 | transport, host, port, path = m.groups() |
| 879 | if transport == "https": |
| 880 | tls = True |
| 881 | else: |
| 882 | tls = False |
| 883 | |
| 884 | path = path or "/" |
| 885 | port = port and int(port) |
| 886 | |
| 887 | # Connect (or reuse) socket |
| 888 | self._connect_or_reuse(host, port=port, tls=tls, timeout=timeout) |
| 889 | |
| 890 | # Build request |
| 891 | if ((tls and port != 443) or |
| 892 | (not tls and port != 80)): |
| 893 | host_hdr = "%s:%d" % (host, port) |
| 894 | else: |
| 895 | host_hdr = host |
| 896 | |
| 897 | headers.setdefault("Host", host_hdr) |
| 898 | headers.setdefault("Path", path) |
| 899 | |
| 900 | if not http_headers: |
| 901 | http_headers = { |
| 902 | "Accept_Encoding": b"gzip, deflate", |
| 903 | "Cache_Control": b"no-cache", |
| 904 | "Pragma": b"no-cache", |
| 905 | "Connection": b"keep-alive", |
| 906 | } |
| 907 | else: |
| 908 | http_headers = {k.replace("-", "_"): v for k, v in http_headers.items()} |
| 909 | http_headers.update(headers) |
| 910 | req = HTTP() / HTTPRequest(**http_headers) |
no test coverage detected