Util to perform an HTTP request. :param host: the host to connect to :param path: the path of the request (default /) :param port: the port (default 80/443) :param timeout: timeout before None is returned :param display: display the result in the default browser (default Fa
(
host, path="/", port=None, timeout=3, display=False, tls=False, verbose=0, **headers
)
| 976 | |
| 977 | |
| 978 | def http_request( |
| 979 | host, path="/", port=None, timeout=3, display=False, tls=False, verbose=0, **headers |
| 980 | ): |
| 981 | """ |
| 982 | Util to perform an HTTP request. |
| 983 | |
| 984 | :param host: the host to connect to |
| 985 | :param path: the path of the request (default /) |
| 986 | :param port: the port (default 80/443) |
| 987 | :param timeout: timeout before None is returned |
| 988 | :param display: display the result in the default browser (default False) |
| 989 | :param iface: interface to use. Changing this turns on "raw" |
| 990 | :param headers: any additional headers passed to the request |
| 991 | |
| 992 | :returns: the HTTPResponse packet |
| 993 | """ |
| 994 | client = HTTP_Client(HTTP_AUTH_MECHS.NONE, verb=verbose) |
| 995 | if port is None: |
| 996 | port = 443 if tls else 80 |
| 997 | ans = client.request( |
| 998 | "http%s://%s:%s%s" % (tls and "s" or "", host, port, path), |
| 999 | timeout=timeout, |
| 1000 | ) |
| 1001 | |
| 1002 | if ans: |
| 1003 | if display: |
| 1004 | if Raw not in ans: |
| 1005 | warning("No HTTP content returned. Cannot display") |
| 1006 | return ans |
| 1007 | # Write file |
| 1008 | file = get_temp_file(autoext=".html") |
| 1009 | with open(file, "wb") as fd: |
| 1010 | fd.write(ans.load) |
| 1011 | # Open browser |
| 1012 | if WINDOWS: |
| 1013 | os.startfile(file) |
| 1014 | else: |
| 1015 | with ContextManagerSubprocess(conf.prog.universal_open): |
| 1016 | subprocess.Popen([conf.prog.universal_open, file]) |
| 1017 | return ans |
| 1018 | |
| 1019 | |
| 1020 | # Bindings |
nothing calls this directly
no test coverage detected