Python |
|||||
Python - HTTP - http.server
The http.server module in Python is part of the standard library and provides classes for implementing HTTP servers (Web servers). It can serve as a simple HTTP server for testing or prototyping web applications, serving static files, or developing small-scale web applications. This module supports HTTP/1.0 and HTTP/1.1.
There are several components in the http.server module and followings are the list and descriptions of those modules :
NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x NOTE 2 : All the examples in this page are assumed to be written/run on Windows 10 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.
< Example 01 > ========================================================
The http.server module also provides a command-line interface for running a simple HTTP server, which can be invoked using the following command: python -m http.server [port] [--bind address] [--directory path] [--cgi]
type in the command as follows with local ip and the port 8080
It prints a line of string indicating that the server is running
Open a browser and type in the server IP and port that you specified above. By default, the http.server would generate Directory listing as shown below.
< Example 02 > ========================================================
Now run the server as shown below
Open a browser and go to the server url as shown below. The webserver sends the contents of the html file that was generated by self.wfile.write().
The server console prints the client information that was retrieved by do_GET(self) function
< Example 03 > ========================================================
|
|||||