HTTP & Web Servers
HTTP (Hypertext Transfer Protocol) can be described as a language that web browsers and web servers speak to each other.
An HTTP transaction involves a client and a server. Web browsers are HTTP clients. A web browser sends HTTP requests to web servers, and servers send responses back to the browser.
Demonstrate a Web Server
There is a Python module http.server that can run a built-in web server on your computer. If you would like to demonstrate a web server, open up a terminal;
cd to a directory that has some files in it (it could be a directory containing some text files, HTML files, or images ) then run
python3 -m http.server 8000 in your terminal. Now you can access the files in that directory from http://localhost:8000/ .
The IPv4 address 127.0.0.1 and the IPv6 address ::1 are “this computer’s” IP address. That means a client (like your browser) is accessing a server on your own computer.
HTTP URIs imply a port number of 80, whereas HTTPS URIs imply a port number of 443. The Python web server mentioned above is running on port 8000. Since this isn’t the default port, you have to write the port number in URIs for it. (http://localhost:8000/)
HTTP GET requests
The GET method means retrieve whatever information is identified by the Request-URI.
GET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1
http://www.w3.org/pub/WWW/TheProject.html is the resource being requested.
HTTP/1.1 is the protocol of the request. Over the years, there have been several changes to the way HTTP works. Clients have to tell servers which dialect of HTTP they're speaking. HTTP/1.1 is the most common version today.
To make a http request by hand you can use the ncat command. While
python3 -m http.server 8000
is running on a terminal. Open up a new terminal and run:
ncat 127.0.0.1 8000
Press enter and paste these two lines:
GET / HTTP/1.1
Press Enter twice. As soon as you do, the response from the server will be displayed on your terminal.
The first line of text that the server sends back is the status line. The numbers in this line (HTTP/1.0 200 OK) are HTTP status codes. The other lines up until the first blank line are the headers. The response body is the rest.
HTTP POST Request
The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.
To show the response of POST Methods, you can save the following html text in your computer and open the file with your browser. Before you submit anything run this command in order to listen on port 8000;
ncat -l 8000
<form action="http://localhost:8000/" method="POST">
<label>Give me the input:
<input type="text" name="inp" value="">
If you post something you get a response like this:
HTTP HEAD Request
The HEAD method is functionally similar to GET, except that the server replies with a response line and headers, but no entity-body.
HTML PUT Request
The PUT method is used to request the server to store the included entity-body at a location specified by the given URL.
There are more HTML methods that mentioned here.
When a client fills in a web form ,this client
POSTs to a server to create or update a resource. The server makes a change to the database and enters the information from the web form there. Typically, the server will send a confirmation page back to the client. This redirect causes the client to GET the created or updated resource.
Cookies are a way that a server can ask a browser to retain a piece of information, and send it back to the server when the browser makes subsequent requests. Cookies are just plain text that can be used by analytics and advertising systems to track user activities. Cookies are also sometimes used to store user preferences for a site. If you look cookies in your browser, you see that there are expiration dates to show when the cookie will be destroyed.
For more detailed information about HTTP, take a look at these:
Udacity’s Course (HTTP&Web Servers): https://classroom.udacity.com/courses/ud303