Solution du TD 6

In [1]:
! ls -lR
.:
total 24
drwxr-xr-x 2 jyt institut 4096 nov.  13  2020 cgi-bin
-rw-r--r-- 1 jyt institut    0 nov.  13  2020 csv.lck
-rw-r--r-- 1 jyt institut  293 nov.  13  2020 participants.csv
-rw-r--r-- 1 jyt institut  123 nov.  13  2020 participants.txt
-rw-r--r-- 1 jyt institut 1351 nov.  13  2020 registration.html
-rw-r--r-- 1 jyt institut  244 nov.  13  2020 server.py
-rw-r--r-- 1 jyt institut    0 nov.  13  2020 txt.lck
-rw-r--r-- 1 jyt institut 2601 nov.  10 08:10 Untitled.ipynb

./cgi-bin:
total 16
-rwxr-xr-x 1 jyt institut 1547 nov.  13  2020 register.cgi
-rwxr-xr-x 1 jyt institut 1547 nov.  13  2020 register.cgi~
-rwxr-xr-x 1 jyt institut  313 nov.  13  2020 test.cgi
-rwxr-xr-x 1 jyt institut  291 nov.  13  2020 test.cgi~

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)
In [ ]: