index.cgi - gopher-web-gateway - A simple gopher gateway for the web generating TXT or PDF output.
 (HTM) git clone git://bitreich.org/gopher-web-gateway git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/gopher-web-gateway
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
       index.cgi (1384B)
       ---
            1 #!/usr/bin/env python
            2 # coding=utf-8
            3 #
            4 # © 2025-2026 Christoph Lohmann <20h@r-36.net>
            5 #
            6 # This file is published under the terms of the GPLv3.
            7 #
            8 
            9 import os
           10 import sys
           11 import getopt
           12 import requests
           13 import html2text
           14 
           15 def main(args):
           16         if len(args) != 7:
           17                 sys.stderr.write("Must be run as geomyidae dcgi.\n")
           18                 return 1
           19         g_search = args[1]
           20         g_arguments = args[2]
           21         g_host = args[3]
           22         g_port = args[4]
           23         g_traversal = args[5]
           24         g_base = g_selector = args[6]
           25         if len(g_traversal) > 0:
           26                 g_base = g_selector[:-len(g_traversal)]
           27         g_usetls = os.getenv("GOPHERS", "off")
           28         if g_usetls == "on":
           29                 g_uri = "gophers://"
           30         else:
           31                 g_uri = "gopher://"
           32         g_uri += g_host
           33         if g_port != "70":
           34                 g_uri += ":" + g_port
           35         g_uri += g_base
           36 
           37         # Mandatory for wikipedia.
           38         headers = { "User-Agent": "gopher mediawiki frontend/0.1" }
           39 
           40         if len(g_search) > 0:
           41                 page = g_search
           42         else:
           43                 page = "/".join(g_traversal.split("/")[1:])
           44 
           45         if len(page) == 0:
           46                 sys.stdout.write("3No URI given\t\t\r\n")
           47                 sys.stdout.flush()
           48                 return 1
           49 
           50         try:
           51                 html = requests.get(page, headers=headers)
           52         except requests.exceptions.MissingSchema:
           53                 sys.stdout.write("3Missing schema\t\t\r\n")
           54                 sys.stdout.flush()
           55                 return 1
           56 
           57         h2t = html2text.HTML2Text()
           58         h2t.ignore_links = True
           59         h2t.ignore_images = True
           60         gph = h2t.handle(html.text)
           61         sys.stdout.write(gph)
           62         sys.stdout.flush()
           63         return 0
           64 
           65 if __name__ == "__main__":
           66         sys.exit(main(sys.argv))
           67