Creating Webservers in Python

Creating Webservers in Python
0

Web Servers

Web servers respond to Hypertext Transfer Protocol (HTTP) requests from clients and send back a response containing a status code and often content such as HTML, XML or JSON as well.

Why are web servers necessary?

Web servers are the ying to the web client’s yang. The server and client speak the standardized language of the World Wide Web. This standard language is why an old Mozilla Netscape browser can still talk to a modern Apache or Nginx web server, even if it cannot properly render the page design like a modern web browser can.

The basic language of the Web with the request and response cycle from client to server then server back to client remains the same as it was when the Web was invented by Tim Berners-Lee at CERN in 1989. Modern browsers and web servers have simply extended the language of the Web to incorporate new standards.

Web server implementations

The conceptual web server idea can be implemented in various ways. The following web server implementations each have varying features, extensions and configurations.

  • The Apache HTTP Server has been the most commonly deployed web server on the Internet for 20+ years.
  • Nginx is the second most commonly used server for the top 100,000 websites and often serves as a reverse proxy for Python WSGI servers.
  • Caddy is a newcomer to the web server scene and is focused on serving the HTTP/2 protocol with HTTPS.
  • rwasa is a newer web server written in Assembly with no external dependencies that tuned to be faster than Nginx. The benchmarks are worth taking a look at to see if this server could fit your needs if you need the fastest performance trading off for as of yet untested web server.

Client requests

A client that sends a request to a web server is usually a browser such as Internet Explorer, Firefox, or Chrome, but it can also be a

  • headless browser, commonly use for testing, such as phantomjs
  • commandline utility, for example wget and cURL
  • text-based web browser such as Lynx
  • web crawler.

Web servers process requests from the above clients. The result of the web server’s processing is a response code and commonly a content response. Some status codes, such as 204 (No content) and 403 (Forbidden), do not have content responses.

In a simple case, the client will request a static asset such as a picture or JavaScript file. The file sits on the file system in a location the web server is authorized to access and the web server sends the file to the client with a 200 status code. If the client already requested the file and the file has not changed, the web server will pass back a 304 “Not modified” response indicating the client already has the latest version of that file.

from flask import Flask
app = Flask(name)
@app.route(’/’)
def hello_world():
return ‘Hello, World!’

if name == ‘main’:
app.run() very simple and easy