The user agent string is an important identifier that gets sent with each HTTP request made by urllib and other libraries in Python. By default, Python uses a generic user agent that simply states it is a "Python-urllib" request. However, you may want to customize this string for various reasons.
Why Customize the User Agent
Here are some common reasons you may want to override the default user agent string:
How to Customize the urllib User Agent
Customizing the user agent with urllib is simple. When you create the request, just add a
import urllib.request
url = 'http://www.example.com'
headers = {'User-Agent': 'MyBot 1.0'}
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
You can customize this user agent string however you want - adding application details, versioning, OS info, etc. Getting the format right is key for proper impersonation.
Other Considerations
Keep in mind that some sites may not react well to user agent impersonation if taken too far. Mimicking browsers and devices is OK, but claiming to be another piece of software or service likely crosses an ethical line.
Additionally, note that other Python HTTP clients allow user agent customization as well, including the popular
I hope this gives you a better understanding of how to override and customize user agent strings with Python's urllib