Skip to content

USER AGENTS - Setting custom user agent

Python
#!/usr/bin/env python3
"""USER AGENTS - Setting custom user agent"""
import urllib.request
print("User Agent Examples:")
user_agents = [
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
    'MyBot/1.0',
    'Python-urllib/3.11'
]
for ua in user_agents:
    req = urllib.request.Request('http://httpbin.org/user-agent')
    req.add_header('User-Agent', ua)
    print(f"  Using: {ua[:30]}...")
    try:
        with urllib.request.urlopen(req) as resp:
            print(f"    Status: {resp.status}")
    except:
        print("    Error")