mirror of
https://bitbucket.org/mangelo/snippets.git
synced 2024-11-21 18:31:00 +02:00
Python CGI servers.
This commit is contained in:
parent
1d82df2991
commit
735bb8d085
102
linux/lighttpd-python-cgi.txt
Normal file
102
linux/lighttpd-python-cgi.txt
Normal file
@ -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('<html><head>')
|
||||||
|
print('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />')
|
||||||
|
print('<meta http-equiv="Expires" content="0">')
|
||||||
|
print('<meta http-equiv="Last-Modified" content="0">')
|
||||||
|
print('<meta http-equiv="Pragma" content="no-cache">')
|
||||||
|
print('<meta http-equiv="Cache-Control" content="no-cache, mustrevalidate">')
|
||||||
|
print('<title>MyTitle</title>')
|
||||||
|
print('</head><body>')
|
||||||
|
print('This is a test CGI page.')
|
||||||
|
print('</body></html>')
|
||||||
|
print('')
|
||||||
|
return
|
||||||
|
|
||||||
|
ShowHtml()
|
||||||
|
|
||||||
|
* End.
|
Loading…
Reference in New Issue
Block a user