wiki:SujetTP7-2018

Version 1 (modified by franck, 10 years ago) (diff)

--

Serveur Web minimaliste

Arborescence du site de base

.
├── server.py*
└── www
    ├── cgi
    │   ├── charts.py*
    │   └── command.py*
    ├── img
    │   └── peri.png
    ├── index.html
    └── peri.html

server.py

#!/usr/bin/env python
import BaseHTTPServer
import CGIHTTPServer
import cgitb; cgitb.enable() 

server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8000)
handler.cgi_directories = ["/cgi"]

httpd = server(server_address, handler)
httpd.serve_forever()

index.html

<html>
 <head><title>Peri Web Server</title></head>
 <frameset rows="100,*" frameborder=0>
  <frame src="img/peri.png">
  <frame src="peri.html" />
 </frameset>
</html>

peri.html

<html>
<head><title>Peri Web Server</title></head>
<body>
<h1>Controle Arduino</h1>

LED:<br/>
<form method="post" action="cgi/command.py">
<input name="val1" cols="20"></input>
<input type="submit" value="Entrer">
</form>

Statistiques:<br/>
<form method="post" action="cgi/charts.py">
<input name="val1" cols="20"></input>
<input type="submit" value="Entrer">
</form>
</body>
</html>

command.py

#!/usr/bin/env python
import cgi
form = cgi.FieldStorage()
val1 = form.getvalue('val1')

print """
<html>
<body>
<p><a href="/peri.html">home</a></p>

La valeur est ... %s<br/>
</body>
</html>
""" % (val1,)

charts.py

#!/usr/bin/env python
import cgi
form = cgi.FieldStorage()
#val1 = (int)(form.getvalue('val1'))
val1 = 20
tab = [10,12,20,70,60,40]

print """
<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['heure', 'niveau']"""
i=0
while i <= val1:
  print "         ,['%d', %d]" % (i,tab[i/4])
  i += 4
print """\
        ]);

        var options = {
          title: 'lumiere',
          hAxis: {title: 'heure',  titleTextStyle: {color: '#333'}},
          vAxis: {minValue: 0, maxValue: 100}
        };

        var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <p><a href="/peri.html">home</a></p>
    <div id="chart_div" style="width: 600; height: 300px;"></div>
  </body>
</html>
"""