How to be an efficient learner

A Very common phenomenon observed while studying is that you are learning but you are not able to remember what you learned. For example- not remembering the theorems of maths, concepts of physics…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Build Web Server From Scratch With Python.

If you are reading an article with this kind of title so maybe your are involved in the world of Web Development and have at least an idea of how web works, if not you can get an overview of it in this article .

The first thing that might come to your head is “Why to create a Web Server from scratch? What inventing the wheel again for?”

A famous thinker used to say: I hear and I forget; I see and I remember; I do and I understand.
I believe to become a better developer you must get a better understanding of the underlying software systems you use on a daily basis and that includes programming languages, compilers and interpreters, databases and operating systems, web servers and web frameworks. And, to get a better and deeper understanding of those systems you must re-build them from scratch, brick by brick, wall by wall.

I hope at this point you’re convinced that it’s a good idea to start re-building different software systems to learn how they work.

But, What is a Web Server then?

It is basically a networking server that sits on a physical server (yeah, a server on a server lol) and waits for a client to send a request. When it receives a request, it generates a response and sends it back to the client. The communication between a client and a server happens using HTTP protocol. A client can be your browser or any other software that speaks HTTP.

The socket must be bound to an address and listening for connections. For that we use the socket.accept() property, The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.

To receive the data from the socket we use the socket.recv() method, The return value is a bytes object representing the data received. The maximum amount of data to be received at once is specified by its argument. Decode to utf-8 can help us to avoid problems with the data received.

Then we set the response and send it using the sendall method and close the connection.

Now let’s try it…

The entire code would be:

If you execute this program and check in your browser you should get something like this:

You can also simulate a web browser running the Web server fire up a telnet session on the command line specifying a host to connect to localhost and the port to connect to 8888 and then press Enter:

At this point you’ve established a TCP connection with the server running on your local host and ready to send and receive HTTP messages. In the picture below you can see a standard procedure a server has to go through to be able to accept new TCP connections.

And that’s the basic model of how a Web server works. To sum it up: The Web server creates a listening socket and starts accepting new connections in a loop. The client initiates a TCP connection and, after successfully establishing it, the client sends an HTTP request to the server and the server responds with an HTTP response that gets displayed to the user. To establish a TCP connection both clients and servers use sockets.

Add a comment

Related posts:

Pros And Cons Of Living in Georgetown Texas

Georgetown is the 7th fastest growing city in the United States and is located just 30miles from Downtown Austin. Let’s talk about the good, the bad, and the ugly. Don’t let this small-town fool you…