Python: Send a request to a web page, and print the headers information
Python Requests: Exercise-4 with Solution
Write a Python code to send a request to a web page, and print the information of headers. Also parse these values and print key-value pairs holding various information.
Sample Solution:
Python Code:
import requests
r = requests.get('https://api.github.com/')
response = r.headers
print("Headers information of the said response:")
print(response)
print("\nVarious Key-value pairs information of the said resource and request:")
print("Date: ",r.headers['date'])
print("server: ",r.headers['server'])
print("status: ",r.headers['status'])
print("cache-control: ",r.headers['cache-control'])
print("vary: ",r.headers['vary'])
print("x-github-media-type: ",r.headers['x-github-media-type'])
print("access-control-expose-headers: ",r.headers['access-control-expose-headers'])
print("strict-transport-security: ",r.headers['strict-transport-security'])
print("x-content-type-options: ",r.headers['x-content-type-options'])
print("x-xss-protection: ",r.headers['x-xss-protection'])
print("referrer-policy: ",r.headers['referrer-policy'])
print("content-security-policy: ",r.headers['content-security-policy'])
print("content-encoding: ",r.headers['content-encoding'])
print("X-Ratelimit-Remaining: ",r.headers['X-Ratelimit-Remaining'])
print("X-Ratelimit-Reset: ",r.headers['X-Ratelimit-Reset'])
print("X-Ratelimit-Used: ",r.headers['X-Ratelimit-Used'])
print("Accept-Ranges:",r.headers['Accept-Ranges'])
print("X-GitHub-Request-Id:",r.headers['X-GitHub-Request-Id'])
Sample Output:
Headers information of the said response: {'date': 'Tue, 20 Oct 2020 13:16:08 GMT', 'content-type': 'application/json; charset=utf-8', 'server': 'GitHub.com', 'status': '200 OK', 'cache-control': 'public, max-age=60, s-maxage=60', 'vary': 'Accept, Accept-Encoding, Accept, X-Requested-With, Accept-Encoding', 'etag': 'W/"27278c3efffccc4a7be1bf315653b901b14f2989b2c2600d7cc2e90a97ffbf60"', 'x-github-media-type': 'github.v3; format=json', 'access-control-expose-headers': 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset', 'access-control-allow-origin': '*', 'strict-transport-security': 'max-age=31536000; includeSubdomains; preload', 'x-frame-options': 'deny', 'x-content-type-options': 'nosniff', 'x-xss-protection': '1; mode=block', 'referrer-policy': 'origin-when-cross-origin, strict-origin-when-cross-origin', 'content-security-policy': "default-src 'none'", 'content-encoding': 'gzip', 'X-Ratelimit-Limit': '60', 'X-Ratelimit-Remaining': '58', 'X-Ratelimit-Reset': '1603201759', 'X-Ratelimit-Used': '2', 'Accept-Ranges': 'bytes', 'Content-Length': '496', 'X-GitHub-Request-Id': 'D2B5:6593:83D581:B5FE5E:5F8EE317'} Various Key-value pairs information of the said resource and request: Date: Tue, 20 Oct 2020 13:16:08 GMT server: GitHub.com status: 200 OK cache-control: public, max-age=60, s-maxage=60 vary: Accept, Accept-Encoding, Accept, X-Requested-With, Accept-Encoding x-github-media-type: github.v3; format=json access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset strict-transport-security: max-age=31536000; includeSubdomains; preload x-content-type-options: nosniff x-xss-protection: 1; mode=block referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin content-security-policy: default-src 'none' content-encoding: gzip X-Ratelimit-Remaining: 58 X-Ratelimit-Reset: 1603201759 X-Ratelimit-Used: 2 Accept-Ranges: bytes X-GitHub-Request-Id: D2B5:6593:83D581:B5FE5E:5F8EE317
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python code to send a request to a web page, and print the response text and content. Also get the raw socket response from the server.
Next: Write a Python code to send a request to a web page, and print the JSON value of the response. Also print each key value of the response.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework