How Does Server and Workstation Communicate in DJANGO
How Does Server and Workstation Communicate in DJANGO
HTML
CSS Django / Flask
DOM Sqlite3 / MySQL
JavaScript
JQuery
http://data.pr4e.org/page1.htm
Getting Data from the Server
• Each time the user clicks on an anchor tag with an href
= value to switch to a new page, the browser makes a
connection to the web server and issues a “GET”
request - to GET the content of the page at the specified
URL.
• The server returns the HTML document to the browser,
which formats and displays the document to the user.
Web Server
Browser
App
Click Parse/
Render
Network Sockets
Phone calls for pairs of applications
Network Sockets
Image source:
http://en.wikipedia.org/wiki/Tin_can_telephone
http://www.flickr.com/photos/kitcowan/2103850699/
TCP Connections / Sockets
Application Application
Process
Internet Process
http://en.wikipedia.org/wiki/Internet_socket
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
http://en.wikipedia.org/wiki/TCP_and_UDP_port
www.dj4e.com
74.208.28.177
TCP Port Numbers
Incoming E-Mail 25
Login 23
blah blah
Web Server 80 blah blah
Clipart: http://www.clker.com/search/networksym/1
HyperText Transfer
Protocol
Wandering through linked documents on the Internet
Uniform Resource Locator
http://data.pr4e.org/page1.htm
• 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
INTERNET PROTOCOL
DARPA INTERNET PROGRAM
• The standards for all of the Internet PROTOCOL
SPECIFICATION
protocols (inner workings) are September 1981
developed by an organization
Internet Standards
Network Working Group
Request for Comments: 2616
Obsoletes: 2068
R. Fielding
UC Irvine
J. Gettys
Category: Standards Track Compaq/W3C
J. Mogul
Compaq
H. Frystyk
W3C/MIT
L. Masinter
Xerox
P. Leach
Microsoft
T. Berners-Lee
W3C/MIT
June 1999
Copyright Notice
Abstract
5.1 Request-Line
HTTP/1.1 200 OK
Date: Thu, 04 Jan 2018 14:45:10 GMT
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Mon, 15 May 2017 11:11:47 GMT
Content-Type: text/html Browser
<h1>The First Page</h1>
<p>If you like, you can switch to
the <a href="http://www.dr-chuck.com/page2.htm">Second
Page</a>.</p>
Connection closed by foreign host.
Accurate Hacking
in the Movies
• Matrix Reloaded
• Bourne Ultimatum
• Die Hard 4
• ...
http://nmap.org/movies.html
Simple "Browser" in Python
The World's Simplest Browser
import socket
while True:
data = mysock.recv(512)
if len(data) < 1:
break
print(data.decode(),end='')
mysock.close()
https://www.dj4e.com/code/http/socket1.py
$ python3 socket1.py
Browser
App
Click Parse/
Render
Web Server
Request and Response Cycle
?????
80
Request Response
<h1>The Second
Browser Page</h1><p>If you like,
GET App
http://data.pr4e.org/page2.ht you can switch back to the
m <a href="page1.htm">First
Page</a>.</p>
The World's
from socket import *
def createServer():
serversocket = socket(AF_INET, SOCK_STREAM)
Server while(1):
(clientsocket, address) = serversocket.accept()
rd = clientsocket.recv(5000).decode()
pieces = rd.split("\n")
if ( len(pieces) > 0 ) : print(pieces[0])
except KeyboardInterrupt :
print("\nShutting down...\n");
except Exception as exc :
print("Error:\n");
print(exc)
serversocket.close()
print('Access http://localhost:9000')
createServer()
https://www.dj4e.com/code/http/server.py
Browser / Server Communication
$ pwd
dj4e/code/http
$ python3 server.py
Access http://localhost:9000
GET / HTTP/1.1
GET /favicon.ico HTTP/1.1
https://www.dj4e.com/code/http/server.py
A Very Simple Web Client
import socket
while True:
data = mysock.recv(512)
if len(data) < 1:
break
print(data.decode(),end='')
mysock.close()
https://www.dj4e.com/code/http/client1.py
Client / Server Communication
$ pwd
dj4e/code/http
$ python3 server.py
Access http://localhost:9000
GET http://127.0.0.1/romeo.txt HTTP/1.0
$ python3 client1.py
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
<html><body>Hello World</body></html>
$
An Even Simpler Web Client
import urllib.request
fhand = urllib.request.urlopen('http://127.0.0.1:9000/romeo.txt')
for line in fhand:
print(line.decode().strip())
$ python3 server.py
Access http://localhost:9000
GET http://127.0.0.1/romeo.txt HTTP/1.0
$ python3 client2.py
<html><body>Hello World</body></html>
https://www.dj4e.com/code/http/client2.py
Browser / Django
Communication