Python
Aller à la navigation
Aller à la recherche
Creer un mini serveur Web qui repond toujours avec un statut 200 sur le port 8080
Source : https://github.com/tanzilli/playground/blob/master/python/httpserver/example1.py
Creer un fichier 8080.py avec le contenu ci-dessous :
#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import socket
PORT_NUMBER = 8080
#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
# Send the html message
hostname = socket.gethostname()
self.wfile.write(hostname + " : Hello World !\n")
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()
Pour le demarrer :
python 8080.py
Lors d'une tenative d'acces celui-ci ecrira sur la console comme ceci :
Started httpserver on port 8081
xx.xx.xx.xx - - [13/Dec/2018 14:02:24] "GET /favicon.ico HTTP/1.1" 200 -
xx.xx.xx.xx - - [13/Dec/2018 14:02:25] "GET / HTTP/1.1" 200 -