! ls -lR
D'abord, le formulaire :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<meta charset="utf-8"/>
<TITLE>Inscription au 6ème congrès de la FFFF</TITLE>
<META http-equiv="Content-Script-Type" content="text/javascript">
</HEAD>
<BODY>
<H1 ALIGN=CENTER>Fédération Française de Friture à Froid </H1>
<H2 ALIGN=CENTER>Inscription au 6ème Congrès</H2>
<HR>
<FORM ACTION="http://localhost:8888/cgi-bin/register.cgi" METHOD="POST" onsubmit="return false">
<TABLE ALIGN="CENTER">
<TR><TD></TD></TR>
<TR><TD>Nom </TD><TD> <INPUT TYPE="TEXT" NAME="nom" SIZE=40></TD></TR>
<TR><TD>Prénom </TD><TD><INPUT TYPE="TEXT" NAME="prenom" SIZE=40></TD></TR>
<TR><TD>Email </TD><TD> <INPUT TYPE="TEXT" NAME="email" SIZE=40></TD></TR>
<TR><TD>Société </TD><TD> <INPUT TYPE="TEXT" NAME="institution" SIZE=40></TD></TR>
<TR><TD>Téléphone </TD><TD> <INPUT TYPE="TEXT" NAME="phone" SIZE=40></TD></TR>
<TR><TD>Arrivée</TD><TD></TD></TR>
<TR><TD>(date et heure)</TD><TD> <INPUT TYPE="TEXT" NAME="arrivee" SIZE=40></TD></TR>
<TR><TD>Depart</TD><TD></TD></TR>
<TR><TD>(date et heure)</TD><TD> <INPUT TYPE="TEXT" NAME="depart" SIZE=40></TD></TR>
</TABLE>
<hr>
<p> <input TYPE="button" VALUE=" Envoyer " onclick="this.form.submit( )" />
</form>
</body>
</html>
Ensuite, dans le répertoire cgi-bin
, le script :
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import cgi, string, sys, os, fcntl
# pour remplacer sauts de lignes et tabulations par des espaces
tt = str.maketrans(string.whitespace, ' '*6)
# lecture des donnees du formulaire
form = cgi.FieldStorage()
# format pour la page de confirmation de l'inscription
html = """
<TITLE> Confimation d'inscription </TITLE>
<H1> Inscription enregistrée </H1>
<H2> 6ème congrès de la FFFF </H2>
<BR>
<BR> %(nom)s %(prenom)s
<BR> %(email)s
<BR>
<BR> %(institution)s
<BR> %(phone)s
<BR> Arrivée : %(arrivee)s
<BR> Depart : %(depart)s
<BR>
<HR>
"""
data = {}
fields = ['nom', 'prenom',
'email', 'institution', 'phone',
'arrivee', 'depart']
for field in fields:
if field not in form:
data[field] = ''
else:
data[field] = form[field].value
row = '|'.join([data[field] for field in fields]).translate(tt) +'|'+'\n'
# ecriture fichier csv (exclusion mutuelle par fichier de lock)
lockfile = open('csv.lck', 'r')
fcntl.flock(lockfile.fileno(), fcntl.LOCK_EX)
f = open('participants.csv','a')
f.write(row)
f.close()
fcntl.flock(lockfile.fileno(), fcntl.LOCK_UN)
# ecriture liste de participants
participant = data['nom']+' '+data['prenom'] +' : ' + data['email']+'\n'
lockfile = open('txt.lck', 'r')
fcntl.flock(lockfile.fileno(), fcntl.LOCK_EX)
f = open('participants.txt','a')
f.write(participant)
f.close()
fcntl.flock(lockfile.fileno(), fcntl.LOCK_UN)
print("Content-type: text/html; charset=utf-8\n")
print (html % data)