0% found this document useful (0 votes)
42 views27 pages

Network Programming

The document discusses HTTP and working with the web. It covers HTTP requests and responses, using urllib to make requests in Python, response objects, status codes for handling failures, HTTP headers for customizing requests, and content compression. Key points include how to make HTTP requests, access response data and metadata, check status codes, add and access headers, and request compressed responses.

Uploaded by

yahia el-siraffy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views27 pages

Network Programming

The document discusses HTTP and working with the web. It covers HTTP requests and responses, using urllib to make requests in Python, response objects, status codes for handling failures, HTTP headers for customizing requests, and content compression. Key points include how to make HTTP requests, access response data and metadata, check status codes, add and access headers, and request compressed responses.

Uploaded by

yahia el-siraffy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Network Programming

Lecture 2
HTTP and Working with the Web

• The Hypertext Transfer Protocol (HTTP) is probably the most widely-used


application layer protocol.
• It was originally developed to allow academics to share HTML documents.
• Nowadays, it is used as the core protocol of innumerable applications across
the Internet,
• it is the principle protocol of the World Wide Web.
Request and response

• An HTTP exchange consists of two elements.


• A request made by the client, which asks the server for a particular resource
specified by a URL,
• A response, sent by the server, which supplies the resource that the client
has asked for.
• If the server can't provide the resource that the client has requested, then
the response will contain information about the failure.
Requests with urllib

• The urllib package is broken into several submodules for dealing with the
different tasks that we may need to perform when working with HTTP.
• For making requests and receiving responses, we employ the urllib.request
module.
Requests with urllib

• Retrieving the contents of a URL is a straightforward process when done


using urllib. Load your Python interpreter and do the following:
Response objects

• urlopen() returns an http.client.HTTPResponse instance.


• The response object gives us access to the data of the requested resource,
and the properties and the metadata of the response.
• To view the URL for the response that we received in the previous section,
do this:
Response objects

• We get the data of the requested resource through a file-like interface using
the readline() and read() methods.
• This is how we use the read() method:
Response objects

• The file-like interface is limited. Once the data has been read, it's not
possible to go back and re-read it by using either of the aforementioned
functions. To demonstrate this, try doing the following:
Status codes

• What if we wanted to know whether anything unexpected had happened to


our request?
• what if we wanted to know whether our response contained any data before
we read the data out?
• Maybe we're expecting a large response, and we want to quickly see if our
request has been successful without reading the whole response.
Status codes

• HTTP responses provide a means for us to do this through status codes. We


can read the status code of a response by using its status attribute.

Status codes are integers that tell us how the request went.
Handling problems

• Any status code in the 200 range indicates a success, whereas any code in either the
400 range or the 500 range indicates failure.
Handling problems

• If something goes wrong lower in the network stack,


• then the appropriate module will raise an exception.
• The urllib package catches these exceptions and then wraps them as
URLErrors.
• For example, we might have specified a host or an IP address that doesn't
exist, as shown here:
HTTP headers

• Requests, and responses are made up of two main parts, headers and a
body.
• We briefly saw some HTTP headers when we used our TCP RFC
downloader in Chapter 1, Network Programming and Python.
• Headers are the lines of protocol-specific information that appear at
the beginning of the raw message that is sent over the TCP connection.
• It is separated from the body by a blank line.
HTTP headers

• Here's an example of an HTTP request:


HTTP headers

• The HTTP protocol allows the client to supply the hostname in the
HTTP request by including a Host header
HTTP headers

• the server can use headers to inform the client about things such as the
length of the body, the type of content the response body contains, and
the cookie data that the client should store
Customizing requests

• Create a Request object


• Add headers to the request object
• Use urlopen() to send the request object
Customizing requests
Customizing requests

• We can check if the response is in Swedish by printing out the first few lines:
Customizing requests

• To view the headers present in a request, do the following:

The urlopen() method adds some of its own headers when we run it on a
request:
Customizing requests

• A shortcut for adding headers is to add them at the same time that we
create the request object, as shown here:
Content compression

• The Accept-Encoding request header and the Content-Encoding response


header can work together to allow us to temporarily encode the body of a
response for transmission over the network.
• Let's discuss how to request a document and get the server to use gzip
compression for the response body.
Content compression

• First, let's construct the request:


>>> req = Request('http://www.debian.org’)
• Next, add the Accept-Encoding header:
>>> req.add_header('Accept-Encoding', 'gzip')
• And then, submit it with the help of urlopen():
>>> response = urlopen(req)
Content compression

• We can check if the server is using gzip compression by looking at the


response's Content-Encoding header:
>>> response.getheader('Content-Encoding')
'gzip'
Content compression

• We can then decompress the body data by using the gzip module:
Content compression

• Let's see what happens if we ask for no compression by using the identity encoding:
Multiple values

If needed, relative weightings can be given to specific encodings by


adding a q value:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy