на просторах інету гітхабу знайшов цей код, запрацювало в мене, з
python -m http.server --cgi 8000
не вийшло, CGI не працював, а який ви сервер запускаєте ? бо в третьому пітоні http.server
▼Прихований текст
#! /usr/bin/env python
'''Run a local cgi server from the current directory that treats *.cgi files
as executable python cgi scripts.'''
import http.server, sys, os
class CGIExtHTTPRequestHandler(http.server.CGIHTTPRequestHandler):
'''This request handler mimics the Loyola server, which looks for CGI files
to end in '.cgi' and be in any directory as opposed to the CGIHTTPServer
expectation that the cgi script are of the form /cgi-bin/*.py.'''
def is_python(self, path):
"""Test whether argument path is a Python script: allow .cgi"""
return path.lower().endswith('.cgi')
def is_cgi(self):
'''As on xenon, go by extension only.'''
base = self.path
query = ''
i = base.find('?')
if i != -1:
query = base[i:]
base = base[:i]
if not base.lower().endswith('.cgi'):
return False
[parentDirs, script] = base.rsplit('/', 1)
self.cgi_info = (parentDirs, script+query)
return True
def run_server():
dirName = os.getcwd()
blanks = dirName.count(' ')
if 0 < blanks: # server cannot handle blanks in path names
print("""The path to this directory contains {blanks} space(s):
{dirName}
Either rename directories to remove the blanks or
move this directory to a place with no blanks in the path.
Aborting the local server run!""".format(**locals()))
input("Press return after reading this message.")
return
server_addr = ('localhost', 8080)
cgiServer = http.server.HTTPServer(server_addr, CGIExtHTTPRequestHandler)
sys.stderr.write('Localhost CGI server started\n.')
cgiServer.serve_forever()
run_server()
підключатись http://localhost:8080
Да і не забувайте, реалізацій CGI багато є, WSGI, FastCGI ... Тому в першу чергу гляньте на то яка саме це реалізація, бо не завжди запрацює, той код який ви знайдете в гуглі, це була моя початкова помилка, та й помилка всіх початківців. Ще скрипт запускати в папці де будуть файли, я її ставлю так
$ ls -l
drwx------+ 1 user Отсутствует 0 авг 1 13:16 cgi-bin
-rwx------+ 1 user Отсутствует 361 июл 28 18:46 index.html
-rwx------+ 1 user Отсутствует 1715 июл 25 16:11 localCGIServer.py
в cgi-bin скрипти cgi.
приклади HTML і так дальше виставляти ?