#!/usr/bin/python # -*- coding: UTF-8 -*- u""" =============================================================================== sitesearch.py - implement local-pages search for a website Synopsis: given search parameters in an HTML form or URL, build a site-specific search query string and send it to a search provider with an HTTP redirect. Author/Copyright: 2016-2024, M. Lutz (learning-python.com) License: provided freely, but with no warranties of any kind. CGI MODULE NOTE: this script relies on the longstanding Python cgi module, which was mind-numbingly deprecated in Python 3.12, and is to be removed in 3.13. This script has no intention of coding the many mods required by this pointless and opinionated removal. Instead, if your server uses Python 3.13 or later, you can thankfully work around this by installing the original cgi module, available on PyPI as package legacy-cgi (see https://pypi.org/project/legacy-cgi/). Simply run the following single command to restore the still-useful cgi and cgitb Python modules: pip3 install legacy-cgi VERSIONS (search on mmm-yyyy for changes): Sep 08, 2024 - Add note above about CGI module deprecation/workaround. Nov 18, 2020 - Error message if parameters absent|invalid, not status 500. Assorted documentation edits/improvements, in this docstr. Sep 26, 2020 - Fix Py3.X+Linux redirect-page print excs on non-ASCII terms. Fix log-file write excs in Py2.X for non-ASCII term encodes. Try but skip alt format for non-ASCII Location search URLs. Add a few privacy-focused newcomers to host selection lists. Jun 28, 2019 - Document and improve the privacy and permissions of the search-terms file saved on the server. Jun 28, 2018 - Improve and describe handling of Unicode in search terms, remove Ixquick (it's now StartPage), add Baidu and Yandex, unescape URL displayed to user in UTF-8 fallback page. Jan 25, 2016 - Log search terms to a server file for metrics, with date and time. For privacy, set this file's Unix permissions to 0200 (or 0600) per "ABOUT PERMISSIONS..." ahead. Jan 15, 2016 - Initial release. ------------------------------------------------------------------------------- OVERVIEW ------------------------------------------------------------------------------- This is a Python CGI script: it runs on a web server, reads form (or URL query) inputs, and prints HTTP headers + HTML text back to the client. To search, this builds a query URL and submits and delegates it to a search provider via an HTTP redirect header. Anonymous search terms are also saved to a file for metrics, and the reply includes a fallback HTML page with redirection options. This CGI script is normally invoked by the "action" tag of the form in the companion HTML page at: http://learning-python.com/sitesearch.html Use your browser's "View Source" to see the form in this HTML page's code; the linkage in the HTML file looks like this: