Tutorial by Examples

Python 2.x2.3 python -m SimpleHTTPServer 9000 Python 3.x3.0 python -m http.server 9000 Running this command serves the files of the current directory at port 9000. If no argument is provided as port number then server will run on default port 8000. The -m flag will search sys.path for ...
Assuming you have the following directory of files: You can setup a web server to serve these files as follows: Python 2.x2.3 import SimpleHTTPServer import SocketServer PORT = 8000 handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer(("localhost"...
What happens when we execute python -m SimpleHTTPServer 9000? To answer this question we should understand the construct of SimpleHTTPServer (https://hg.python.org/cpython/file/2.7/Lib/SimpleHTTPServer.py) and BaseHTTPServer(https://hg.python.org/cpython/file/2.7/Lib/BaseHTTPServer.py). Firstly, P...
# from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer # python2 from http.server import BaseHTTPRequestHandler, HTTPServer # python3 class HandleRequests(BaseHTTPRequestHandler): def _set_headers(self): self.send_response(200) self.send_header('Content-type', 'text...

Page 1 of 1