Last active
February 13, 2018 12:44
-
-
Save bhubr/2855fe15649749b9d5a1ec8a67c57bd1 to your computer and use it in GitHub Desktop.
Mini serveur web Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Not Found</h1> | |
<p>Cette page n'existe pas !</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>About</h1> | |
<p>Python c'est super</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Home</h1> | |
<p>Bienvenue</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from http.server import BaseHTTPRequestHandler, HTTPServer | |
from datetime import datetime | |
html_base = """ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<ul> | |
<li><a href="/">Accueil</a></li> | |
<li><a href="/about">About</a></li> | |
</ul> | |
{content} | |
</body> | |
</html> | |
""" | |
html_contents = {} | |
html_files = ['index.html', 'about.html', '404.html'] | |
# Pour chaque fichier | |
for file in html_files: | |
# Separer base du nom de fichier ("basename") et extension | |
file_name_ext = file.split('.') | |
# Récupère le basename dans une variable | |
base_name = file_name_ext[0] | |
# Ouvre le fichier en lecture (paramètre "r") | |
fh = open(file, 'r') | |
# Lit tout le contenu du fichier et le met dans content | |
# Puis ferme le fichier | |
content = fh.read() | |
fh.close() | |
# Injecte le contenu à l'index correspondant au basename | |
html_contents[base_name] = content | |
# HTTPRequestHandler class | |
class MiniHTTPServerRequestHandler(BaseHTTPRequestHandler): | |
# GET | |
def do_GET(self): | |
authorized_paths = ['/', '/about', '/dynamic'] | |
# Verifier que le path demandé | |
# status_code = 200 if self.path in authorized_paths else 404 | |
if self.path in authorized_paths: | |
status_code = 200 | |
if self.path == "/": | |
html = html_contents['index'] | |
elif self.path == "/about": | |
html = html_contents['about'] | |
elif self.path == "/dynamic": | |
date = datetime.now() | |
users = [ | |
{ 'name': 'Jon Snow', 'email': 'jonsnow@winterfell.com', 'pw': 'dany' }, | |
{ 'name': 'Arya Stark', 'email': 'arya@winterfell.com', 'pw': 'dagger' }, | |
{ 'name': 'Sansa Stark', 'email': 'sansa@winterfell.com', 'pw': 'killramsay' }, | |
{ 'name': 'Bran Stark', 'email': 'bran@winterfell.com', 'pw': 'raven' } | |
] | |
user_items = [ ('<li>' + u['name'] + '</li>') for u in users ] | |
user_liste = '<ul>' + ''.join(user_items) + '</ul>' | |
html = '<p>Date courante: ' + str(date) + '</p>' + user_liste | |
# html = html_contents['index'] if self.path == "/" else html_contents['about'] | |
else: | |
status_code = 404 | |
html = html_contents['404'] | |
html_content = html_base.format(content=html) | |
# Send response status code | |
self.send_response(status_code) | |
# Send headers | |
self.send_header('Content-type','text/html') | |
self.end_headers() | |
# Write content as utf-8 data | |
self.wfile.write(bytes(html_content, "utf8")) | |
return | |
def run(): | |
print('starting server...') | |
# Server settings | |
# Choose port 8080, for port 80, which is normally used for a http server, you need root access | |
server_address = ('127.0.0.1', 8081) | |
httpd = HTTPServer(server_address, MiniHTTPServerRequestHandler) | |
print('running server...') | |
httpd.serve_forever() | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment