2 minute read

HyperText Transfer Protocol

Wandering through linked documents on the internet

Uniform Resource Locator (URL)

http:// data.pre4e.org /page1.htm

protocol host document

HTTP - HyperText Transfer Protocol

  • The dominant Application Layer Protocol on the internet.
  • Invented for the Web - to retrieve HTML, Images, Documents, etc.
  • Extended to handle data in addition to documents - RSS, Web Services, etc.
  • Basic Concept: Make a connection - Request a document - Retrieve the document - Close the connection
  • Internet and sockets were created in the 1970’s, HTTP was invented in 1990 and is an application protocol that runs atop sockets

Internet Standards

  • The standards for all of the internet protocols (inner workings) are developed by an organization
  • Internet Engineering Task Force (IETF)
  • Standards are called “RFCs” - “Request for Comments”

Network Sockets

Phone calls for pair of applications

TCP Connections / Sockets

In computer networking, an internet socket or network socket is an endpoint of a bidirectional inter-process communication flow across an Internet Protocol-based computer network, such as the internet.

TCP Port Numbers

  • A port is an application-specific or process-specific software communications endpoint
  • It allows multiple networked applications to coexist on the same server
  • There is a list of well-known TCP port numbers

The World’s Simplest Browser

#socket1.py

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #create socket
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/page1.htm HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)

while True:
	data = mysock.recv(512)
	if len(data) < 1:
		break
	print(data.decode(), end='')

mysock.close()

In the server

The World’s Simplest Web Server

#server.py

from socket import *

def createServer():
	serversocket = socket(AF_INET, SOCK_STREAM)
	try:
		serversocket.bind(('localhost', 9000))
		serversocket.listen(5)
		while(1):
			(clientsocket, address) = serversocket.accept()

			rd = clientsocket.recv(5000).decode()
			pieces = rd.split("\n")
			if (len(pieces) > 0): print(pieces[0])

			data = "HTTP/1.1 200 OK\r\n"
			data += "Connect-Type: text/html; charset=utf-8\r\n"
			data += "\r\n"
			data += "<html><body>Hello World</body></html>\r\n\r\n"
			clientsocket.sendall(data.encode())
			clientsocket.shutdown(SHUT_WR)

	except KeyboardInterrupt:
		print("\nShutting down...\n");
	except Exception as exc:
		print("Error:\n");
		print(exc)
	
	serversocket.close()

print("Access http://localhost:9000")
createServer()

A very simple Web Client

#client1.py

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #create socket
mysock.connect(('127.0.0.1', 9000))
cmd = 'GET http://127.0.0.1/romeo.txt HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)

while True:
	data = mysock.recv(512)
	if len(data) < 1:
		break
	print(data.decode(), end='')

mysock.close()

Even Simpler Web Client

#client2.py

import urllib.request

fhand = urllib.request.urlopen('http://127.0.0.1:9000/romeo.txt')
for line in fhand:
	print(line.decode().strip())

The Structure of a Django Application

Django Terminology (I.e. folders)

  • Project is a collection of applications

Flow of a web request

  • When the request arrives at Django app the incoming request URL is compared to the list of paths in urls.py in the variable urlpatterns
  • When there is a url match, it selects a “View” which is a bit of code that handles any database access and then produces and delivers the response to the browser.
  • The view access the database indirectly through an abstraction called a “model
  • This is a general web pattern called “Model-View-Controller” or MVC

Categories:

Updated: