Skip to Content
Mastering Python for Networking and Security
book

Mastering Python for Networking and Security

by José Manuel Ortega
September 2018
Intermediate to advanced
426 pages
10h 46m
English
Packt Publishing
Content preview from Mastering Python for Networking and Security

Checking HTTP headers with urllib2

HTTP requests consist of two main parts: headers and a body. Headers are the lines of information that contain specific metadata about the response that tells the client how to interpret it. With this module we can check whether the headers can provide information about the web server.

The http_response.headers statement provides the header of the web server. Before we access this property, we need to check whether the code response is equal to 200.

You can find the following code in the urllib_headers_basic.py file:

import urllib2url = raw_input("Enter the URL ")http_response = urllib2.urlopen(url)print 'Status Code: '+ str(http_response.code)if http_response.code == 200:    print http_response.headers

In ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Python for Networking and Security - Second Edition

Mastering Python for Networking and Security - Second Edition

José Manuel Ortega
Python for Cybersecurity

Python for Cybersecurity

Howard E. Poston, III

Publisher Resources

ISBN: 9781788992510Supplemental Content