In Python, you can use the urllib library to issue network requests and customize HTTP headers. Here is a step-by-step example of setting HTTP headers using urllib:
First, you need to import the relevant urllib modules:
pythonimport urllib.request
Then, create a Request object and specify the URL and headers to set during initialization:
pythonurl = 'http://example.com/api/data' headers = { 'User-Agent': 'My User Agent 1.0', 'Accept-Language': 'en-US,en;q=0.5', # Additional headers... } request = urllib.request.Request(url, headers=headers)
In the above code, the headers dictionary contains the headers to set, such as User-Agent and Accept-Language. These headers are included when creating the Request object.
Next, you can use the urlopen method to issue the request:
pythontry: response = urllib.request.urlopen(request) print(response.status) # Output response status code print(response.getheaders()) # Output response headers content = response.read() # Read response content print(content) # Print response content except urllib.error.URLError as e: print('Error occurred:', e.reason)
In the above example, if the request succeeds, the response status code, headers, and content are printed. If an error occurs (e.g., network issues or HTTP errors), the URLError exception is caught and the error reason is displayed.
Additionally, to modify or add headers dynamically, you can use the add_header method on the Request object:
pythonrequest.add_header('Authorization', 'Bearer <YOUR_ACCESS_TOKEN>')
This approach allows flexible addition or modification of HTTP headers for specific requests.
Note that urllib is a module in Python 3; in Python 2, urllib2 is used instead. The above code is written using Python 3 syntax and module structure.