403 lines
39 KiB
HTML
403 lines
39 KiB
HTML
<!DOCTYPE html>
|
||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
|
||
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
|
||
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
|
||
<!--[if gt IE 8]><!-->
|
||
<html class="no-js" lang="fr"><!--<![endif]--><head>
|
||
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta charset="utf-8"><meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"><title>Créer un serveur web rapidement en python</title><meta content="Apprendre à créer un serveur web rapidement en python - Python Programmation Cours Tutoriel Informatique Apprendre" name="description"><meta content="width=device-width, initial-scale=1" name="viewport"><meta content="serveur,web,apprendre,créer,rapidement,python" name="Keywords"><meta content="fr" http-equiv="Content-Language"><meta content="index, follow" name="robots"><meta content="All" name="audience"><meta content="All" name="rating"><meta content="2 days" name="revisit-after"><meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"><meta content="yes" name="apple-mobile-web-app-capable"><link href="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/python.png" rel="image_src"><meta content="/images/python.png" property="og:image"><link href="https://fonts.googleapis.com/" rel="preconnect"><link crossorigin="" href="https://fonts.gstatic.com/" rel="preconnect"><link href="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/css2.css" rel="stylesheet"><style>
|
||
@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro&display=swap');
|
||
</style><link href="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/css.css" rel="stylesheet"><style>
|
||
html{padding:0px;margin:0px; }
|
||
a{color:#428bca;text-decoration:none;}
|
||
body{
|
||
margin:0px;
|
||
padding:0px;
|
||
font-family: 'Source Sans Pro', 'Open Sans', sans-serif;
|
||
font-size:17px;
|
||
line-height:1.42857143;
|
||
color:#333;
|
||
background-color:#fff;
|
||
padding-bottom:20px;
|
||
}
|
||
#menu_main li a{font-size:0.9em;}
|
||
ul{margin:0px;}
|
||
#left_menu ul{padding:0px !important;}
|
||
p{margin:0 0 10px;}
|
||
li{list-style-type:none;padding-left:10px;}
|
||
h1, h2, h3{font-weight:500;line-height:1.1;margin-top:30px !important;margin-bottom:10px !important;}
|
||
|
||
ul > li{
|
||
list-style-type:circle;
|
||
}
|
||
|
||
h1{font-size:2em !important;}
|
||
h2{font-size:1.5em !important;}
|
||
#page{max-width:1000px;padding:20px;padding-top:0px;margin-top:0px;}
|
||
#page p{text-align:justify;}
|
||
#page h3{font-size:1.2em !important;margin-top:10px;margin-bottom:10px;}
|
||
.breadcrumb{padding:2px !important;margin-bottom:10px !important;}
|
||
.links_top{
|
||
padding:0px;margin-top:20px;margin-bottom:30px;
|
||
display:block;
|
||
clear:both;
|
||
}
|
||
.links_top > div{
|
||
display:block;
|
||
width:100%;
|
||
}
|
||
.links_top_next{
|
||
text-align:right;
|
||
}
|
||
|
||
|
||
|
||
#menu_main{position:relative;}
|
||
@media (max-width:500px){.float-center{float:none !important;position:relative;clear:both;}
|
||
}
|
||
@media (min-width:993px){#page{margin-left:230px;}
|
||
#menu_main{position:fixed;top:0px;left:0px;width:220px;background-color:#173025;margin-top:0px;padding:0px;padding-left:10px;vertical-align:middle !important;border-right:1px solid #ccc;overflow-x:hidden;}
|
||
#menu_main h3{padding-left:0px;font-size:20px;font-weight:800 !important;line-height:20px !important;}
|
||
#menu_main li{padding:1px;}
|
||
#menu_main li a{width:100%;display:block;color:#eee;}
|
||
#menu_main li a:hover{color:#44B78C;text-decoration:none;}
|
||
}
|
||
@media (max-width:992px){#page{padding:10px;}
|
||
.container{margin:0px;padding:0px;}
|
||
.row{margin:0px;padding:0px;}
|
||
.row > div{margin:0px;padding:0px;}
|
||
#logo_menu{display:none;}
|
||
#menu_main{width:100%;}
|
||
#menu_main h3{width:100%;text-align:center;}
|
||
#menu_main li{width:100%;text-align:center;}
|
||
#menu_main li a{width:100%;text-align:center;padding:2px;display:block;-webkit-transition-property:background-color;-webkit-transition-duration:0.5;-moz-transition-property:background-color;-moz-transition-duration:0.5s;transition-property:background-color;transition-duration:0.5s;width:100%;text-align:center;display:block;padding:6px;background-color:#f8f8f8;border:1px solid #ccc;font-size:1.2em;color:#111;text-decoration:none;margin:-1px 0px 0px -1px;}
|
||
}
|
||
@media (min-width:600px) and (max-width:800px){.row > div{width:50%;float:left;}
|
||
}
|
||
@media (min-width:800px) and (max-width:1000px){.row > div{width:33%;float:left;}
|
||
}
|
||
@media (min-width:992px){.row{width:100%;margin:0px;padding:0px;}
|
||
}
|
||
@media (max-width:992px){#menu_main{height:auto;}
|
||
}
|
||
.breadcrumb{padding:6px 6px;background-color:white !important;}
|
||
.breadcrumb-list{padding-top:15px;padding-bottom:5px;}
|
||
#category_menu{width:100%;max-width:500px;margin:auto;margin-top:40px;}
|
||
#category_menu li{width:100%;}
|
||
#category_menu a{-webkit-transition-property:background-color;-webkit-transition-duration:0.5;-moz-transition-property:background-color;-moz-transition-duration:0.5s;transition-property:background-color;transition-duration:0.5s;width:100%;text-align:center;display:block;padding:6px;background-color:#f8f8f8;border:1px solid #ccc;font-size:1.2em;color:#111;text-decoration:none;margin:-1px 0px 0px -1px;}
|
||
#category_menu a:hover{background-color:white;}
|
||
#pub_right{display:none;text-align:right;}
|
||
@media (min-width:1100px) and (max-width:1500px){
|
||
#pub_right{display:block;width:160px;position:fixed;top:0px;right:0px;}
|
||
}
|
||
@media (min-width:1500px){
|
||
#pub_right{display:block;width:300px;position:fixed;top:0px;right:0px;}
|
||
}
|
||
@media (min-width:1100px){#page{max-width:700px;}
|
||
}
|
||
@media (min-width:1150px){#page{max-width:750px;}
|
||
}
|
||
@media (min-width:1200px){#page{max-width:800px;}
|
||
}
|
||
@media (min-width:1250px){#page{max-width:850px;}
|
||
}
|
||
@media (min-width:1300px){#page{max-width:900px;}
|
||
}
|
||
@media (min-width:1350px){#page{max-width:950px;}
|
||
}
|
||
@media (min-width:1400px){#page{max-width:1000px;}
|
||
}
|
||
@media (min-width:1450px){#page{max-width:1050px;}
|
||
}
|
||
@media (min-width:1500px){#page{max-width:950px;}
|
||
}
|
||
@media (min-width:1550px){#page{max-width:1000px;}
|
||
}
|
||
@media (min-width:1600px){#page{max-width:1050px;}
|
||
}
|
||
@media (min-width:1650px){#page{max-width:1100px;}
|
||
}
|
||
@media (min-width:1700px){#page{max-width:1150px;}
|
||
}
|
||
@media (min-width:1750px){#page{max-width:1200px;}
|
||
}
|
||
@media (max-width:500px){#h1-title-programmation-cours{text-align:center;}
|
||
#python-programmation-logo{float:none;margin:auto;margin-bottom:30px;}
|
||
}
|
||
*{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;}
|
||
pre{
|
||
display:block;padding:9.5px;margin:0 0 10px;font-size:14px;line-height:1.42857143;word-break:break-all;word-wrap:break-word;color:#333;background-color:#f5f5f5;
|
||
border:1px solid #ccc;border-radius:4px;}
|
||
pre{margin-top:20px;margin-bottom:20px;}
|
||
pre{border:none;border-radius:0px;background-color:#efefef;}
|
||
pre {-moz-tab-size: 3 !important; -o-tab-size: 3 !important; tab-size: 3 !important; }
|
||
img { vertical-align: middle; }
|
||
|
||
@media (max-width: 1000px) {
|
||
footer{text-align:center; }
|
||
}
|
||
|
||
|
||
|
||
#menu_top_container {
|
||
display:none;
|
||
position: absolute;
|
||
z-index: 99;
|
||
width: 100%;
|
||
background-color: #0C4B33 !important;
|
||
text-align: center;
|
||
top: 0px;
|
||
}
|
||
|
||
#menu_top a {
|
||
position: relative;
|
||
display: inline-block;
|
||
padding: 2px 15px;
|
||
font-size: 1.8em;
|
||
margin-top: 0px;
|
||
float: left;
|
||
height: 100%;
|
||
color: white;
|
||
font-weight: bold;
|
||
}
|
||
|
||
@media (max-width: 375px) {
|
||
#menu_top a {
|
||
padding: 2px 8px;
|
||
}
|
||
}
|
||
@media (max-width: 300px) {
|
||
#menu_top a {
|
||
padding: 2px 5px;
|
||
}
|
||
}
|
||
@media (max-width: 259px) {
|
||
#menu_top a {
|
||
padding: 2px 1px;
|
||
}
|
||
}
|
||
|
||
form[action="/page-contact"] input,
|
||
form[action="/page-contact"] textarea
|
||
{
|
||
padding:10px;
|
||
border:1px solid #eee;
|
||
outline:none;
|
||
margin:4px auto;
|
||
}
|
||
|
||
a:hover{ text-decoration: none !important; }
|
||
|
||
|
||
@media (max-width: 992px) {
|
||
|
||
#menu_main{ padding:0px; }
|
||
#page{ margin-top:30px; }
|
||
#menu_top_container{ display: block; }
|
||
}
|
||
|
||
strong{
|
||
color: #445580 !important;
|
||
background-color: rgba(232, 227, 227, 0.33) !important;
|
||
font-weight:500 !important;
|
||
}
|
||
|
||
p.legende {
|
||
font-weight: normal !important;
|
||
font-size:0.9em;
|
||
}
|
||
|
||
.python_books_title{
|
||
font-weight:bold;
|
||
}
|
||
.python_books > div{
|
||
border:1px dashed #EEE;
|
||
text-align:center;
|
||
background-color: #E8E8E8;
|
||
padding:10px;
|
||
margin-bottom:20px;
|
||
}
|
||
|
||
.form_send_mail input,
|
||
.form_send_mail textarea {
|
||
padding:5px;
|
||
margin-top:2px;
|
||
}
|
||
|
||
.form_send_textarea {
|
||
min-height:200px;
|
||
}
|
||
|
||
|
||
/* navigation mobile */
|
||
@media (max-width: 500px) {
|
||
.links_top > div{
|
||
padding-bottom:10px;
|
||
}
|
||
|
||
}
|
||
|
||
#left_menu h3{
|
||
color: #44B78C;
|
||
font-weight: bold;
|
||
}
|
||
|
||
|
||
.ps-scrollbar-y-rail{
|
||
display: none !important;
|
||
}
|
||
|
||
.content img{
|
||
display: block;
|
||
margin: 3rem auto;
|
||
max-width: 100%;
|
||
}
|
||
|
||
|
||
|
||
pre { line-height: 125%; }
|
||
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
|
||
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
|
||
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
|
||
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
|
||
.codehilite pre { background-color: #20201d }
|
||
.codehilite pre code { color: #fff2f2 }
|
||
.codehilite .hll { background-color: #49483e }
|
||
.codehilite { background: #272822; color: #f8f8f2 }
|
||
.codehilite .c { color: #75715e } /* Comment */
|
||
.codehilite .err { color: #960050; background-color: #1e0010 } /* Error */
|
||
.codehilite .esc { color: #f8f8f2 } /* Escape */
|
||
.codehilite .g { color: #f8f8f2 } /* Generic */
|
||
.codehilite .k { color: #66d9ef } /* Keyword */
|
||
.codehilite .l { color: #ae81ff } /* Literal */
|
||
.codehilite .n { color: #f8f8f2 } /* Name */
|
||
.codehilite .o { color: #f92672 } /* Operator */
|
||
.codehilite .x { color: #f8f8f2 } /* Other */
|
||
.codehilite .p { color: #f8f8f2 } /* Punctuation */
|
||
.codehilite .ch { color: #75715e } /* Comment.Hashbang */
|
||
.codehilite .cm { color: #75715e } /* Comment.Multiline */
|
||
.codehilite .cp { color: #75715e } /* Comment.Preproc */
|
||
.codehilite .cpf { color: #75715e } /* Comment.PreprocFile */
|
||
.codehilite .c1 { color: #75715e } /* Comment.Single */
|
||
.codehilite .cs { color: #75715e } /* Comment.Special */
|
||
.codehilite .gd { color: #f92672 } /* Generic.Deleted */
|
||
.codehilite .ge { color: #f8f8f2; font-style: italic } /* Generic.Emph */
|
||
.codehilite .gr { color: #f8f8f2 } /* Generic.Error */
|
||
.codehilite .gh { color: #f8f8f2 } /* Generic.Heading */
|
||
.codehilite .gi { color: #a6e22e } /* Generic.Inserted */
|
||
.codehilite .go { color: #66d9ef } /* Generic.Output */
|
||
.codehilite .gp { color: #f92672; font-weight: bold } /* Generic.Prompt */
|
||
.codehilite .gs { color: #f8f8f2; font-weight: bold } /* Generic.Strong */
|
||
.codehilite .gu { color: #75715e } /* Generic.Subheading */
|
||
.codehilite .gt { color: #f8f8f2 } /* Generic.Traceback */
|
||
.codehilite .kc { color: #66d9ef } /* Keyword.Constant */
|
||
.codehilite .kd { color: #66d9ef } /* Keyword.Declaration */
|
||
.codehilite .kn { color: #f92672 } /* Keyword.Namespace */
|
||
.codehilite .kp { color: #66d9ef } /* Keyword.Pseudo */
|
||
.codehilite .kr { color: #66d9ef } /* Keyword.Reserved */
|
||
.codehilite .kt { color: #66d9ef } /* Keyword.Type */
|
||
.codehilite .ld { color: #e6db74 } /* Literal.Date */
|
||
.codehilite .m { color: #ae81ff } /* Literal.Number */
|
||
.codehilite .s { color: #e6db74 } /* Literal.String */
|
||
.codehilite .na { color: #a6e22e } /* Name.Attribute */
|
||
.codehilite .nb { color: #f8f8f2 } /* Name.Builtin */
|
||
.codehilite .nc { color: #a6e22e } /* Name.Class */
|
||
.codehilite .no { color: #66d9ef } /* Name.Constant */
|
||
.codehilite .nd { color: #a6e22e } /* Name.Decorator */
|
||
.codehilite .ni { color: #f8f8f2 } /* Name.Entity */
|
||
.codehilite .ne { color: #a6e22e } /* Name.Exception */
|
||
.codehilite .nf { color: #a6e22e } /* Name.Function */
|
||
.codehilite .nl { color: #f8f8f2 } /* Name.Label */
|
||
.codehilite .nn { color: #f8f8f2 } /* Name.Namespace */
|
||
.codehilite .nx { color: #a6e22e } /* Name.Other */
|
||
.codehilite .py { color: #f8f8f2 } /* Name.Property */
|
||
.codehilite .nt { color: #f92672 } /* Name.Tag */
|
||
.codehilite .nv { color: #f8f8f2 } /* Name.Variable */
|
||
.codehilite .ow { color: #f92672 } /* Operator.Word */
|
||
.codehilite .pm { color: #f8f8f2 } /* Punctuation.Marker */
|
||
.codehilite .w { color: #f8f8f2 } /* Text.Whitespace */
|
||
.codehilite .mb { color: #ae81ff } /* Literal.Number.Bin */
|
||
.codehilite .mf { color: #ae81ff } /* Literal.Number.Float */
|
||
.codehilite .mh { color: #ae81ff } /* Literal.Number.Hex */
|
||
.codehilite .mi { color: #ae81ff } /* Literal.Number.Integer */
|
||
.codehilite .mo { color: #ae81ff } /* Literal.Number.Oct */
|
||
.codehilite .sa { color: #e6db74 } /* Literal.String.Affix */
|
||
.codehilite .sb { color: #e6db74 } /* Literal.String.Backtick */
|
||
.codehilite .sc { color: #e6db74 } /* Literal.String.Char */
|
||
.codehilite .dl { color: #e6db74 } /* Literal.String.Delimiter */
|
||
.codehilite .sd { color: #e6db74 } /* Literal.String.Doc */
|
||
.codehilite .s2 { color: #e6db74 } /* Literal.String.Double */
|
||
.codehilite .se { color: #ae81ff } /* Literal.String.Escape */
|
||
.codehilite .sh { color: #e6db74 } /* Literal.String.Heredoc */
|
||
.codehilite .si { color: #e6db74 } /* Literal.String.Interpol */
|
||
.codehilite .sx { color: #e6db74 } /* Literal.String.Other */
|
||
.codehilite .sr { color: #e6db74 } /* Literal.String.Regex */
|
||
.codehilite .s1 { color: #e6db74 } /* Literal.String.Single */
|
||
.codehilite .ss { color: #e6db74 } /* Literal.String.Symbol */
|
||
.codehilite .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
|
||
.codehilite .fm { color: #a6e22e } /* Name.Function.Magic */
|
||
.codehilite .vc { color: #f8f8f2 } /* Name.Variable.Class */
|
||
.codehilite .vg { color: #f8f8f2 } /* Name.Variable.Global */
|
||
.codehilite .vi { color: #f8f8f2 } /* Name.Variable.Instance */
|
||
.codehilite .vm { color: #f8f8f2 } /* Name.Variable.Magic */
|
||
.codehilite .il { color: #ae81ff } /* Literal.Number.Integer.Long */
|
||
</style></head><body><div id="menu_top_container"><div id="menu_top"><a href="https://python.doctor/"><i class="fa fa-home"></i></a><a href="https://python.doctor/page-cours-python-debutant-documentation" title="Python"><img src="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/logo_python.png"></a><a href="https://python.doctor/page-cours-python-avance-documentation-francaise" title="Python avancé"><img src="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/logo_python_best.png"></a><a href="https://python.doctor/page-django-cours-tutoriels-documentation-francaise" title="Python Django"><img src="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/logo_django.png"></a><a href="https://python.doctor/page-raspberry-pi-apprendre-debutant-acheter-python" title="Python Raspberry pi"><img src="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/logo_raspberry.png" style="margin-right:0px;padding-right:0px;"></a><a href="#" onclick="if (!window.__cfRLUnblockHandlers) return false; go_to_menu();return false;" style="float:right;margin-left:0px;padding-left:0px;"><i class="glyphicon glyphicon-align-justify"></i></a></div></div><div id="pub_right" style="text-align: center; position: fixed; top: 0px;"></div><div id="page"><h1> Créer un serveur web rapidement en python </h1><!-- breadcrumb --><div class="breadcrumb-list" xmlns:v="http://rdf.data-vocabulary.org/#"><span class="breadcrumb" typeof="v:Breadcrumb"><a href="https://python.doctor/" property="v:title" rel="v:url"> Accueil </a></span><span class="chevron"> › </span><span class="breadcrumb" typeof="v:Breadcrumb"><a href="https://python.doctor/page-cours-python-avance-documentation-francaise" property="v:title" rel="v:url" title="Voir tous les articles de cette catégorie"> Python avancé </a></span><span class="chevron"> › </span><span class="breadcrumb-current"><a href="https://python.doctor/page-python-serveur-web-creer-rapidement" style="line-height:2.1em;"> Créer un serveur web rapidement en python </a></span></div><div class="links_top"><div class="links_top_back"><a href="https://python.doctor/page-database-data-base-donnees-query-sql-mysql-postgre-sqlite"><i class="glyphicon glyphicon-chevron-left"></i> Base de données </a></div><div class="links_top_next"><a href="https://python.doctor/page-websocket-python-crossbar-push-socket-publish-subscribe-call-remote-register-wamp"> Websocket & Crossbar <i class="glyphicon glyphicon-chevron-right"></i></a></div></div><div class="content"><p> Il peut être intéressant, dans certains cas, d'implémenter un <strong> serveur web </strong> dans votre application. Cela permet notamment une communication entre vos programmes via un navigateur. En <strong> Python </strong><strong> créer un serveur web </strong> , c'est quelques ligne de code: </p><h2> Serveur web python 2 </h2><p> Voici le code pour créer un serveur web en <u> python 2 </u> : </p><p class="legende"> server.py </p><pre><span class="ch">#!/usr/bin/python</span>
|
||
|
||
<span class="kn">import</span> <span class="nn">BaseHTTPServer</span>
|
||
<span class="kn">import</span> <span class="nn">CGIHTTPServer</span>
|
||
|
||
<span class="n">PORT</span> <span class="o">=</span> <span class="mi">8888</span>
|
||
<span class="n">server_address</span> <span class="o">=</span> <span class="p">(</span><span class="s2">""</span><span class="p">,</span> <span class="n">PORT</span><span class="p">)</span>
|
||
|
||
<span class="n">server</span> <span class="o">=</span> <span class="n">BaseHTTPServer</span><span class="o">.</span><span class="n">HTTPServer</span>
|
||
<span class="n">handler</span> <span class="o">=</span> <span class="n">CGIHTTPServer</span><span class="o">.</span><span class="n">CGIHTTPRequestHandler</span>
|
||
<span class="n">handler</span><span class="o">.</span><span class="n">cgi_directories</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"/"</span><span class="p">]</span>
|
||
<span class="nb">print</span> <span class="s2">"Serveur actif sur le port :"</span><span class="p">,</span> <span class="n">PORT</span>
|
||
|
||
<span class="n">httpd</span> <span class="o">=</span> <span class="n">server</span><span class="p">(</span><span class="n">server_address</span><span class="p">,</span> <span class="n">handler</span><span class="p">)</span>
|
||
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
|
||
</pre><h2> Serveur web python 3 </h2><p> Et voici le code pour créer un serveur web en <u> python 3 </u> : </p><p class="legende"> server.py </p><pre><span class="kn">import</span> <span class="nn">http.server</span>
|
||
|
||
<span class="n">PORT</span> <span class="o">=</span> <span class="mi">8888</span>
|
||
<span class="n">server_address</span> <span class="o">=</span> <span class="p">(</span><span class="s2">""</span><span class="p">,</span> <span class="n">PORT</span><span class="p">)</span>
|
||
|
||
<span class="n">server</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">server</span><span class="o">.</span><span class="n">HTTPServer</span>
|
||
<span class="n">handler</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">server</span><span class="o">.</span><span class="n">CGIHTTPRequestHandler</span>
|
||
<span class="n">handler</span><span class="o">.</span><span class="n">cgi_directories</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"/"</span><span class="p">]</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Serveur actif sur le port :"</span><span class="p">,</span> <span class="n">PORT</span><span class="p">)</span>
|
||
|
||
<span class="n">httpd</span> <span class="o">=</span> <span class="n">server</span><span class="p">(</span><span class="n">server_address</span><span class="p">,</span> <span class="n">handler</span><span class="p">)</span>
|
||
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
|
||
</pre><h2> Créer une page web </h2><p> Créez un fichier <strong> index.py </strong> à la racine de votre projet. </p><p class="legende"> index.py </p><pre><span class="c1"># coding: utf-8</span>
|
||
|
||
<span class="kn">import</span> <span class="nn">cgi</span>
|
||
|
||
<span class="n">form</span> <span class="o">=</span> <span class="n">cgi</span><span class="o">.</span><span class="n">FieldStorage</span><span class="p">()</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Content-type: text/html; charset=utf-8</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
|
||
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">getvalue</span><span class="p">(</span><span class="s2">"name"</span><span class="p">))</span>
|
||
|
||
<span class="n">html</span> <span class="o">=</span> <span class="s2">"""<!DOCTYPE html></span>
|
||
<span class="s2"><head></span>
|
||
<span class="s2"> <title>Mon programme</title></span>
|
||
<span class="s2"></head></span>
|
||
<span class="s2"><body></span>
|
||
<span class="s2"> <form action="/index.py" method="post"></span>
|
||
<span class="s2"> <input type="text" name="name" value="Votre nom" /></span>
|
||
<span class="s2"> <input type="submit" name="send" value="Envoyer information au serveur"></span>
|
||
<span class="s2"> </form> </span>
|
||
<span class="s2"></body></span>
|
||
<span class="s2"></html></span>
|
||
<span class="s2">"""</span>
|
||
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">html</span><span class="p">)</span>
|
||
</pre><p> Ouvrez votre navigateur et indiquez-lui l'url de votre serveur web, dans notre cas ce sera <strong> localhost:8888/index.py </strong></p><p><img class="shadow-bordered" src="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/index-python-serveur-web.png"></p><p> Indiquez votre nom puis cliquez sur le bouton " <strong> Envoyer </strong> ": </p><p><img class="shadow-bordered" src="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/index-py-serveur-web-2.png"></p><p>
|
||
A noter que python ne fait pas de différences entre POST et GET, vous
|
||
pouvez passer une variable dans l'url le résultat sera le même: </p><pre>http://localhost:8888/index.py?name=Olivier%20ENGEL
|
||
</pre><h2> Afficher les erreurs sur votre page web </h2><p> Vous pouvez ajouter une fonction qui affichera les erreurs rencontrées par python (mode debug): </p><pre><span class="kn">import</span> <span class="nn">cgitb</span><span class="p">;</span> <span class="n">cgitb</span><span class="o">.</span><span class="n">enable</span><span class="p">()</span>
|
||
</pre><h2> Afficher les variables d'environnement </h2><p> Vous pouvez afficher toutes les informations concernant votre serveur web / page en cours en appelant la méthode <strong> test() </strong> : </p><pre>cgi.test()
|
||
</pre><hr><p> Pour approfondir le sujet: <a href="https://docs.python.org/3/library/cgi.html" target="_blank" title="Doc python sur CGI"> Doc python CGI <i class="fa fa-external-link"></i></a></p></div><div class="links_top"><div class="links_top_back"><a href="https://python.doctor/page-database-data-base-donnees-query-sql-mysql-postgre-sqlite"><i class="glyphicon glyphicon-chevron-left"></i> Base de données </a></div><div class="links_top_next"><a href="https://python.doctor/page-websocket-python-crossbar-push-socket-publish-subscribe-call-remote-register-wamp"> Websocket & Crossbar <i class="glyphicon glyphicon-chevron-right"></i></a></div></div><div style="background-color: #6d6d6d ; clear:both; height:50px; padding:5px;"><a href="https://python.doctor/page-cours-python-debutant-documentation" style="float:left;width:25%;text-align:center" title="Python"><img src="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/logo_python.png"></a><a href="https://python.doctor/page-cours-python-avance-documentation-francaise" style="float:left;width:25%;text-align:center" title="Python avancé"><img src="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/logo_python_best.png"></a><a href="https://python.doctor/page-django-cours-tutoriels-documentation-francaise" style="float:left;width:25%;text-align:center" title="Python Django"><img src="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/logo_django.png"></a><a href="https://python.doctor/page-raspberry-pi-apprendre-debutant-acheter-python" style="float:left;width:25%;text-align:center" title="Python Raspberry pi"><img src="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/logo_raspberry.png" style="margin-right:0px;padding-right:0px;"></a></div><br ;="" style="clear:both"><div class="container ps-container ps-active-y" id="menu_main" style="height: 100%;"><a href="https://python.doctor/" id="logo_menu" title="Cours python pour débutants et experts"><img src="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/python.png" title="Apprendre python django cours débutant raspberry pi tutoriels "></a><br><!-- Left menu --><div id="left_menu"><div><div><h3> Python débutant </h3><ul><li><a href="https://python.doctor/" title="Python language de programmation apprendre cours tutoriaux django informatique dévelopement web internet script"> Présentation Python </a></li><li><a href="https://python.doctor/page-apprendre-installer-python-ordinateur" title="Installer Python sur votre ordinateur"> Installer Python </a></li><li><a href="https://python.doctor/page-utiliser-interpreteur-python" title="Interpréteur python"> Interpréteur python </a></li><li><a href="https://python.doctor/page-editeurs-python-gratuits-payants-ide" title=" Editeurs IDE Python"> IDE Editeurs python </a></li><li><a href="https://python.doctor/page-apprendre-variables-debutant-python" title="Calculs et variables python"> Calculs et variables </a></li><li><a href="https://python.doctor/page-apprendre-listes-list-tableaux-tableaux-liste-array-python-cours-debutant" title="Les listes python"> Les listes </a></li><li><a href="https://python.doctor/page-apprendre-tuples-tuple-python" title="Les tuples python"> Les tuples </a></li><li><a href="https://python.doctor/page-apprendre-dictionnaire-python" title="Les dictionnaires python"> Les dictionnaires </a></li><li><a href="https://python.doctor/page-apprendre-creer-fonction-en-python" title="Les fonctions python"> Les fonctions </a></li><li><a href="https://python.doctor/page-builtin-built-in-fonctions-internes-python" title="Les fonctions natives de python"> Les fonctions natives </a></li><li><a href="https://python.doctor/page-apprendre-conditions-structures-conditionnelles-if-else-python-cours-debutant" title="IF ELIF ELSE Python Conditions"> Conditions if elif else </a></li><li><a href="https://python.doctor/page-apprendre-boucles-python-loop" title="Les boucles for et while Python"> Boucle for / while </a></li><li><a href="https://python.doctor/page-python-modules-package-module-cours-debutants-informatique-programmation" title="Les modules et les packages en python"> Les modules/packages </a></li><li><a href="https://python.doctor/page-apprendre-exceptions-except-python-cours-debutant" title="Les exceptions python"> Les exceptions </a></li><li><a href="https://python.doctor/page-comprehension-list-listes-python-cours-debutants" title="Les compréhensions de liste python"> Les compréhensions de liste </a></li><li><a href="https://python.doctor/page-apprendre-programmation-orientee-objet-poo-classes-python-cours-debutants" title="Programmation orientée objet en python / classes"> Programmation orientée objet </a></li><li><a href="https://python.doctor/page-decorateurs-decorator-python-cours-debutants" title="Les décorateurs python"> Les décorateurs </a></li><li><a href="https://python.doctor/page-iterateurs-iterator-generateur-generator-python" title="Les itérateurs et les générateurs python"> Les itérateurs/générateurs </a></li><li><a href="https://python.doctor/page-expressions-regulieres-regular-python" title="Les expressions régulières en python"> Expressions régulières </a></li><li><a href="https://python.doctor/page-lire-ecrire-creer-fichier-python" title="Créer lire et écrire dans un fichier en python"> Lire / Editer un fichier </a></li><li><a href="https://python.doctor/page-pep-8-bonnes-pratiques-coder-python-apprendre" title="PEP 8 apprendre les bonnes pratiques pour coder en python"> PEP 8 / bonnes pratiques </a></li><li><a href="https://python.doctor/page-black-code-formatter" title="Black, le formateur de code python sans compromis"> Black formateur de code </a></li></ul></div><div><h3> Python avancé </h3><ul><li><a href="https://python.doctor/page-syntaxe-differente-python2-python3-python-differences" title="Différences entres python 2 et python 3"> Différences python 2 / 3 </a></li><li><a href="https://python.doctor/page-python-encodage-encode-decode-unicode-ascii-codec-character-accents-probleme-string-utf8" title="Encodage python"> Encodage python </a></li><li><a href="https://python.doctor/page-pip-installer-librairies-automatiquement" title="Pip installe vos librairies en toute simplicité"> Pip installe vos librairies </a></li><li><a href="https://python.doctor/page-virtualenv-python-environnement-virtuel" title="Environnement virtuel en python"> Virtualenv </a></li><li><a href="https://python.doctor/page-debogueur-debugger-debug-python-ipython-pdb-pdbpp-cours-tutoriel-script" title="Debug Python ipdb"> Debug </a></li><li><a href="https://python.doctor/page-python-path-pythonpath" title="Python path"> Python Path </a></li><li><a href="https://python.doctor/page-gestion-fichiers-dossiers-python" title="Gestion des fichiers et des dossiers en python"> Gestion fichiers et dossiers </a></li><li><a href="https://python.doctor/page-tkinter-interface-graphique-python-tutoriel" title="Interface graphique Tkinter python"> Interface graphique tKinter </a></li><li><a href="https://python.doctor/page-creer-graphiques-scientifiques-python-apprendre" title="Créer des graphiques scientifiques avec python"> Graphiques scientifiques </a></li><li><a href="https://python.doctor/page-programmation-asynchrone-python-thread-threading" title="La programmation asynchrone avec python"> Programmation asynchrone </a></li><li><a href="https://python.doctor/page-xml-python-xpath" title="XML et python"> XML et python </a></li><li><a href="https://python.doctor/page-beautifulsoup-html-parser-python-library-xml" title="BeautifulSoup / parser vos XML et HTML"> BeautifulSoup / HTML parser </a></li><li><a href="https://python.doctor/page-cx_freeze-creer-executables-python-cours-apprendre" title="Créer des exécutables python avec cx_Freeze"> Créer un exécutable </a></li><li><a href="https://python.doctor/page-dossier-partage-samba-reseau-python-fstab" title="Dossier partagé / samba"> Dossier partagé / samba </a></li><li><a href="https://python.doctor/page-python-ftp-creer-copier-voir-dossier-folder-fichiers" title="FTP et SFTP"> FTP / SFTP </a></li><li><a href="https://python.doctor/page-python-ssh-connect-host-distant-sftp-os-fabric-client" title="Fabric SSH python"> Fabric SSH </a></li><li><a href="https://python.doctor/page-python-envoyer-mail-smtp" title="Envoyer un mail par SMTP en python"> Envoyer un mail SMTP </a></li><li><a href="https://python.doctor/page-reseaux-sockets-python-port" title="Python réseau socket"> Réseau / socket </a></li><li><a href="https://python.doctor/page-database-data-base-donnees-query-sql-mysql-postgre-sqlite" title="Base de données et Python"> Base de données </a></li><li><a href="https://python.doctor/page-python-serveur-web-creer-rapidement" title="Créer un serveur web rapidement en python"> Créer un serveur web </a></li><li><a href="https://python.doctor/page-websocket-python-crossbar-push-socket-publish-subscribe-call-remote-register-wamp" title="Websocket Python avec Crossbar"> Websocket & Crossbar </a></li><li><a href="https://python.doctor/page-generateur-site-statique-python-django" title="Générateur de sites statiques"> Générateur de site statique </a></li><li><a href="https://python.doctor/page-numba-python" title="Python & Numba"> Numba </a></li><li><a href="https://python.doctor/page-tests-unitaires" title="Tests unitaire & python"> Tests unitaires </a></li><li><a href="https://python.doctor/page-deep-learning-python" title="Deep learning & python"> Deep Learning IA </a></li><li><a href="https://python.doctor/page-deep-learning-pytorch" title="Deep learning PyTorch & python"> Deep Learning & PyTorch </a></li><li><a href="https://python.doctor/page-block-chain-presentation-introduction-securite" title="Block Chain"> Block Chain </a></li></ul></div><div><h3> Django </h3><ul><li><a href="https://python.doctor/page-django-introduction-python" title="Présentation de django"> Présentation Django </a></li><li><a href="https://python.doctor/page-django-installer-python" title="Installer django"> Installer Django </a></li><li><a href="https://python.doctor/page-django-initialiser-creer-python" title="Initialiser un projet django"> Initialiser un projet Django </a></li><li><a href="https://python.doctor/page-django-application-creer-python" title="Créer une application Django"> Créer une application Django </a></li><li><a href="https://python.doctor/page-django-orm-apprendre-base-donnees-database-queryset-modeles" title="ORM Django"> ORM Django </a></li><li><a href="https://python.doctor/page-django-login-authentification-de-base-apprendre-creer-cours-tutoriel-python" title="Login Django"> Login Django </a></li><li><a href="https://python.doctor/page-django-champs-modeles-orm-model" title="Le champ des modèles Django"> Les champs des modèles </a></li><li><a href="https://python.doctor/page-django-interface-admin-administration-settings-django-contrib-auth" title="Interface admin Django"> Interface admin Django </a></li><li><a href="https://python.doctor/page-django-query-set-queryset-manager" title="Django Queryset"> Queryset </a></li><li><a href="https://python.doctor/page-django-many-to-many-manytomany-relation-relationships-orm" title="Many to Many Django"> Many to many relation </a></li><li><a href="https://python.doctor/page-django-vues-views-generic-generiques-python" title="Les vues Django"> Les vues </a></li><li><a href="https://python.doctor/page-django-formulaires-forms-champs-fields" title="Les formulaires Django"> Les formulaires </a></li><li><a href="https://python.doctor/page-django-csrf-token-securite-formulaires-python-ajax-post-jquery-anguarjs" title="CSRF Token sécurité Django"> CSRF Token </a></li><li><a href="https://python.doctor/page-django-middlewares-vues-views-request-response-process-before-after-avant-apres" title="Middleware Django"> Les middlewares </a></li><li><a href="https://python.doctor/page-django-templates-structure-template-HTML-variables-boucles-tags-filtre-page-dry-application-projet-modifier-dossier" title="Templates Django"> Templates Django </a></li><li><a href="https://python.doctor/page-django-context-processor-template" title="Context Processor Django"> Context Processor </a></li><li><a href="https://python.doctor/page-django-signaux-signal-signals-python-cours-apprendre-tutoriel-example" title="Signaux Django"> Signaux Django </a></li><li><a href="https://python.doctor/page-django-xadmin-bootstrap-twitter-theme-export-xls-csv-xml-python-web-plugin" title="Xadmin Django thème bootstrap twitter"> Xadmin </a></li><li><a href="https://python.doctor/page-django-admin-select2-admin-ajax-many-to-many-foreign-key-form-widget-django_select2" title="Select2 et Django"> Django select2 </a></li><li><a href="https://python.doctor/page-django-crispy-form-formulaire-bootstrap-twitter-html-fieldset-inline-dynamique-template" title="Crispy Form Django"> Crispy Forms </a></li><li><a href="https://python.doctor/page-django-angularjs-angular-js-coffee-script-coffeescript-python-cours-tuto-tutoriel-exemples" title="AngularJS et Django"> AngularJS et Django </a></li><li><a href="https://python.doctor/page-django-comment-uploader-fichier-image-cours-apprendre-python-upload-file" title="Upload un fichier en Ajax avec Django"> Uploader un fichier en ajax </a></li><li><a href="https://python.doctor/page-django-rest-framework-drf-cours-tuto-tutoriel-exemples" title="Django REST Framework DRF API"> Django Rest Framework </a></li><li><a href="https://python.doctor/page-django-deploiement-python-mise-en-production-linux-gunicorn-supervisor-nginx-installer-configurer-serveur-http-web" title="Django déploiement Gunicorn Supervisor Nginx WSGI"> Django déploiement </a></li></ul></div><div><h3> Raspberry Pi </h3><ul><li><a href="https://python.doctor/page-raspberry-pi-presentation-python-c-est-quoi-apprendre-nano-ordinateur-video" title="Présentation raspberry pi"> Présentation Raspberry pi </a></li><li><a href="https://python.doctor/page-raspberry-pi-install-installer-debian-linux-windows-ubuntu" title="Installer raspbian (OS linux) sur raspberry pi"> Installer Raspbian </a></li><li><a href="https://python.doctor/page-raspberry-pi-adresse-ip-statique-address-static" title="Addresse IP statique / raspberry pi"> IP statique </a></li><li><a href="https://python.doctor/page-raspberry-pi-partage-share-dossier-folder-samba-debian" title="Partager un dossier debian / raspberry pi"> Samba / partager un dossier </a></li><li><a href="https://python.doctor/page-raspberry-pi-lecture-lire-video-1080p-full-hd-omxplayer-ligne-commande" title="OMXPlayer FULL HD 1080px raspberry pi"> Lire vidéo 1080p </a></li><li><a href="https://python.doctor/page-raspberry-pi-installer-vpn-dns-openvpn-debian-python" title="Installer VPN raspberry pi"> Installer VPN </a></li><li><a href="https://python.doctor/page-raspberry-pi-installer-client-torrent-transmission-download-dl-python" title="Installer et utiliser un client torrent"> Client torrent </a></li></ul></div><div><h3> Site </h3><ul><li><a href="https://python.doctor/page-contact" title="Contact"> Contacter l'auteur </a></li><li><a href="https://python.doctor/page-mentions-legales" title="Mentions légales"> Mentions légales </a></li></ul></div></div></div><div class="ps-scrollbar-x-rail" style="width: 209px; display: none; left: 0px;"><div class="ps-scrollbar-x" style="left: 0px; width: 0px;"></div></div><div class="ps-scrollbar-y-rail" style="top: 1195px; height: 912px; display: inherit;"><div class="ps-scrollbar-y" style="top: 455px; height: 347px;"></div></div></div><!-- /container --><hr><div style="clear:both;"><footer><p style="text-align:center;"> Apprendre programmation cours python 3 <br> Django internet web - Documentation débutant et expert <br><a href="https://python.doctor/en/" hreflang="en" title="Python Django"> Version anglaise </a></p><p></p><p></p></footer></div><!-- end page --></div><script type="text/javascript">
|
||
//<![CDATA[
|
||
var __current_page = './page-python-serveur-web-creer-rapidement';
|
||
//]]>
|
||
</script><!-- Opti google --><noscript id="deferred-styles"><link href=/css/all.css?v2 rel=stylesheet type=text/css></noscript><script async="" src="Cr%C3%A9er%20un%20serveur%20web%20rapidement%20en%20python_fichiers/main.js" type="text/javascript">
|
||
</script></body></html> |