HTTP protocol


Intro

What for

Python libraries

HTTP message format (both request > and response <)

$ curl reisinge.net -v
* Rebuilt URL to: reisinge.net/
*   Trying 109.230.20.210...
* Connected to reisinge.net (109.230.20.210) port 80 (#0)
> GET / HTTP/1.1
> Host: reisinge.net
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Server: nginx/1.12.2
< Date: Thu, 11 Jan 2018 07:51:16 GMT
< Content-Type: text/html
< Content-Length: 161
< Connection: keep-alive
< Location: http://jreisinger.github.io
<
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
* Connection #0 to host reisinge.net left intact

The client can’t issue another request over the same socket until the response is finished.

Methods

GET

POST

Encoding

HTTP transfer encoding <-> content encoding

Transfer encoding (Content-Length or chunked encoding, raw or compressed)

GET / HTTP/1.1
Accept-Encoding: gzip
...

HTTP/1.1 200 OK
Content-Length: 3913
Transfer-Encoding: gzip
...

Content type - what format will be selected to represent a given resource

Content encoding - if the format ^ is text, what encoding will be used to turn text code points into bytes

Content-Type: text/html; charset=utf-8

Authentication and cookies

Basic Auth (HTTP-mediated authentication)

TLS/SSL

Cookies

GET /login HTTP/1.1
...

HTTP/1.1 200 OK
Set-Cookie: session-id=d41d8cd98f00b204e9800998ecf8427e; Path=/
...

GET /login HTTP/1.1
Cookie: session-id=d41d8cd98f00b204e9800998ecf8427e
...

Keep-Alive

Various

Minimally correct request nowadays (otherwise 404):

GET /html/rfc7230 HTTP/1.1
Host: tools.ietf.org

Status codes - returned by a server with each response

>>> r = urlopen('http://httpbin.org/status/301')
>>> r.status, r.url
(200, 'http://httpbin.org/get')
>>>
>>> r = requests.get('http://httpbin.org/status/301')
>>> (r.status_code, r.url)
(200, 'http://httpbin.org/get')
>>> r.history
[<Response [301]>, <Response [302]>]

Caching headers

Sources

Foundations of Python Network Programming (2014) - ch. 9

comments powered by Disqus