diff --git a/linux/lighttpd-python-cgi.txt b/linux/lighttpd-python-cgi.txt new file mode 100644 index 0000000..24bec73 --- /dev/null +++ b/linux/lighttpd-python-cgi.txt @@ -0,0 +1,102 @@ +Lighttpd: CGI with Python +------------------------------ + +* Enable CGI in lighttpd: + + * Create: /etc/lighttpd/conf-available/95-testing-cgi.conf + + #-- Alias. + alias.url += ( "/testing" => "/home/user/path/to/testcgi/dir" ) + + #-- Server. + $HTTP["url"] =~ "^/testing" { + cgi.assign = ("" => "") + } + + * Enable the configuration. + + ln -s /etc/lighttpd/conf-available/95-testing-cgi.conf /etc/lighttpd/conf-enabled/95-testing-cgi.conf + + * Restart lighttpd: + + systemctl restart lighttpd.service + + +* Simple python http cgi server: + + tree + +-- cgi-bin + | +-- retrieval.py + +-- forms.html + + chmod +x cgi-bin/retrieval.py + python -m CGIHTTPServer 8088 + http://localhost:8000/forms.html + + +* Simple python https cgi server: + + tree + +-- retrieval.py + +-- forms.html + + * Generate the https certificate: + openssl req -x509 -newkey rsa:4096 -keyout servercgi.pem -out servercgi.pem -days 3650 -nodes + + * servercgi.py: + #!/usr/bin/env python3 + import cgitb, getpass, http.server, os, ssl, sys + + PUERTO = 9099 + + def Main(): + nombre = getpass.getuser() + if (nombre != 'myusername'): + print('ERROR: use only with the user myusername.') + return + + os.environ['PYTHONDONTWRITEBYTECODE'] = 'TRUE' + os.chdir('/path/to/tree') + cgitb.enable() + server = http.server.HTTPServer + handler = http.server.CGIHTTPRequestHandler + srvaddr = ('', PUERTO) + handler.cgi_directories = ['/'] + srvobj = server(srvaddr, handler) + srvobj.socket = ssl.wrap_socket(srvobj.socket, certfile='servercgi.pem', server_side=True) + handler.have_fork=False + print('Page: https://127.0.0.1:' + str(PUERTO) + '/retrieval.py') + srvobj.serve_forever() + return + + Main() + + * Start the server with ./servercgi.py + + * Testing script: /path/to/tree/retrieval.py + + #! /usr/bin/env python3 + import cgi, cgitb + + cgitb.enable(format='html') + #cgitb.enable(format='text') + + def ShowHtml(): + print('Content-Type: text/html; charset=UTF-8') + print('') + print('') + print('') + print('') + print('') + print('') + print('') + print('MyTitle') + print('') + print('This is a test CGI page.') + print('') + print('') + return + + ShowHtml() + + * End.