Bonjour,

J'ai pour projet personnel de faire un site web tout en ayant des contraintes assez élevés lié à mon environnement.

-Je dois utilisé du python 2.6
-J'utilise les librairies de bases de python

J'ai pas mal de questions sur l'organisation et sur la réalisation du site.

Au niveau de l'architecture je souhaite :

- Serveur :
En python 2.6 avec la librairie BaseHTTPServer + SQLite

Mon code de base (récupéré) est le suivant...

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from os import curdir, sep, path
import cgi
 
PORT_NUMBER = 36654
 
#This class will handles any incoming request from
#the browser 
class myHandler(BaseHTTPRequestHandler):
 
	#Handler for the GET requests
	def do_GET(self):
		print "do_GET"
		if self.path=="/":
			self.path="/index_example3.html"
 
		try:
			#Check the file extension required and
			#set the right mime type
 
			sendReply = False
			if self.path.endswith(".html"):
				mimetype='text/html'
				sendReply = True
			if self.path.endswith(".jpg"):
				mimetype='image/jpg'
				sendReply = True
			if self.path.endswith(".gif"):
				mimetype='image/gif'
				sendReply = True
			if self.path.endswith(".js"):
				mimetype='application/javascript'
				sendReply = True
			if self.path.endswith(".css"):
				mimetype='text/css'
				sendReply = True
 
			if sendReply == True:
				#Open the static file requested and send it
				f = open(curdir + sep + self.path) 
				self.send_response(200)
				self.send_header('Content-type',mimetype)
				self.end_headers()
				self.wfile.write(f.read())
				print "Chargement 1"
				f.close()
			return
 
		except IOError:
			self.send_error(404,'File Not Found: %s' % self.path)
 
	#Handler for the POST requests
	def do_POST(self):
		if self.path=="/send":
			form = cgi.FieldStorage(
				fp=self.rfile, 
				headers=self.headers,
				environ={'REQUEST_METHOD':'POST',
		                 'CONTENT_TYPE':self.headers['Content-Type'],
			})
 
			print "Your name is: %s" % form["your_name"].value
			self.path="/index_example5.html"
			self.do_GET()
			return			
		if self.path == "/send2":
			form = cgi.FieldStorage(
                                fp=self.rfile,
                                headers=self.headers,
                                environ={'REQUEST_METHOD':'POST',
                                 'CONTENT_TYPE':self.headers['Content-Type'],
                        })
 
                        print "Your name is: %s" % form["your_name"].value
                        self.path="/index.html"
 
			self.do_GET()
 
                        return
 
 
try:
	#Create a web server and define the handler to manage the
	#incoming request
	server = HTTPServer(('', PORT_NUMBER), myHandler)
	print 'Started httpserver on port ' , PORT_NUMBER
 
	#Wait forever for incoming htto requests
	server.serve_forever()
 
except KeyboardInterrupt:
	print '^C received, shutting down the web server'
	server.socket.close()
-Client :
Le client se réalisé avec du python, de l'html, ajax, js, bootsrap.


Mes premières questions sont les suivantes :

- Comment intégrer du python dans de l'HTML ? Est ce que mon extension doit être .py et comment gérer la redirection (je pense notamment au ligne suivante)

Quid avec du python ? Dois je faire une fonction python qui retourne un string ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
self.wfile.write(f.read()
Quid avec l'extension .py ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sendReply = False
			if self.path.endswith(".html"):
				mimetype='text/html'
				sendReply = True
			if self.path.endswith(".jpg"):
				mimetype='image/jpg'
				sendReply = True
			if self.path.endswith(".gif"):
				mimetype='image/gif'
				sendReply = True
			if self.path.endswith(".js"):
				mimetype='application/javascript'
				sendReply = True
			if self.path.endswith(".css"):
				mimetype='text/css'
				sendReply = True

Je souhaite gérer de l'authentification et la visibilité de certains éléments du site.

Comment gérer la visibilité ? La question rejoint celle d'intégrer du python avec du html...

Quel librairie me conseillez-vous pour l'encodage (et le décodage par conséquence) du mot de passe en base ? Tout en gardant à l'esprit que je suis en 2.6 et que mes librairies doivent être au maximum celle fournit par python de base...

Avez vous des exemples, tutoriels, ... qui me permettraient d'avancer ? Les documentations sur BaseHTTPServer sont très légères et je n'ai trouvé que peu d'exemples...

Je vous remercie d'avance et reste à disposition pour tous compléments d'informations.

Cordialement Quentin.