If you want to add or modify header information when using requests to initiate an HTTP request, just set the headers parameter;

Sample code

When making a GET request, modify the User-Agent:

import requests as r

headers = {
    'User-Agent':'MyProgram'
}

response = r.get("http://example.com",headers=headers)

All Header value types must be string, bytestring or unicode;

Getting the headers of a request

If you want to get the headers of a request, you can use the following properties:

response.request.headers

response.request.headers obtains an instance similar to a dictionary, the key values ​​of which are case-insensitive;

Get the User-Agent of the request, these two methods have the same effect:

response.request.headers['user-agent']
response.request.headers.get('user-agent')

If you want to get the Headers of the server response, you can refer to: