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 Python skills with w3resource's quiz
Python: Tips of the Day
Creates a dictionary with the same keys as the provided dictionary and values generated by running the provided function for each value:
Example:
def tips_map_values(obj, fn): ret = {} for key in obj.keys(): ret[key] = fn(obj[key]) return ret users = { 'Owen': { 'user': 'Owen', 'age': 29 }, 'Eddie': { 'user': 'Eddie', 'age': 15 } } print(tips_map_values(users, lambda u : u['age'])) # {'Owen': 29, 'Eddie': 15}
Output:
{'Owen': 29, 'Eddie': 15}
- New Content published on w3resource:
- 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
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework