Python Forum
urllib2.urlopen() user agent header
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
urllib2.urlopen() user agent header
#1
urllib2.urlopen() sends a user agent HTTP header in the request that identifies the library.  what i want to do is send the request without any user agent header at all (just the host header).  does anyone know how to do that?  i don't see it in the docs. i do see vague (not enough detail to know what to do) instructions on how to change it, but nothing about how to remove it. do i need to just make a plain TCP connection and do my own HTTP?

Output:
User-Agent: Python-urllib/3.5
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You can manipulate user-agent,the easiest way is of course to use Requests(should always be used anyway and not urllib).
λ ptpython 
>>> import requests

>>> r = requests.get("https://httpbin.org/headers", headers={"user-agent": "My cool Useragent" })
>>> print(r.text)
{
 "headers": {
   "Accept": "*/*",
   "Accept-Encoding": "gzip, deflate",
   "Connection": "close",
   "Host": "httpbin.org",
   "User-Agent": "My cool Useragent"
 }
}
I have a post here where i us urllib with FancyURLopener
This post set user-agent with Requests to get access.
Reply
#3
i don't want to set user agent, i want to unset it so the header is absent.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
import urllib2

request = urllib2.Request("http://domain.com", headers={'User-agent': ''})
response = urllib2.urlopen(request).read()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Jun-29-2017, 05:45 AM)Skaperen Wrote: i don't want to set user agent, i want to unset it so the header is absent.
Just try and see how much you can get away with before no connection.
import requests

headers = {
    'user-agent': '',
    'values': '',
    }

r = requests.get("https://httpbin.org/headers", headers=headers)
print(r.text)
Output:
{  "headers": {    "Accept": "*/*",    "Accept-Encoding": "gzip, deflate",    "Connection": "close",      "Host": "httpbin.org",    "User-Agent": "",    "Values": ""  } }
Reply
#6
i guess i will just go ahead and make a plain TCP connection and do minimal HTTP mysel (trivial). i already tried it with the telnet command and the server responded as desired.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
You could just use the socket module.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Jul-01-2017, 12:27 PM)wavic Wrote: You could just use the socket module.
that's how i would make a plain TCP connection.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
https://github.com/python/cpython/blob/2...b2.py#L336 Wrote:
class OpenerDirector:
   def __init__(self):
       client_version = "Python-urllib/%s" % __version__
       self.addheaders = [('User-agent', client_version)]

So the default opener for urllib always includes the user agent, at init.  So there's no option to disable it... but you can just create an opener and clear that header out of it.  Something like this?
import urllib2
opener = urllib2.build_opener()
opener.addheaders = [header for header in opener.addheaders if header[0] != "User-agent"]
urllib2.install_opener(opener)

# now you can use urllib2.open() normally, without the useragent header
data = urllib2.open("https://python-forum.io")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can urlopen be blocked by websites? peterjv26 2 3,380 Jul-26-2020, 06:45 PM
Last Post: peterjv26
  malformed header from script 'main.py': Bad header: * Serving Flask app "main" anuragsapanbharat 2 4,526 Jun-12-2019, 07:26 AM
Last Post: anuragsapanbharat
  SSLCertVerificationError using urllib (urlopen) FalseFact 1 5,884 Mar-31-2019, 08:34 AM
Last Post: snippsat
  Error: module 'urllib' has no attribute 'urlopen' mitmit293 2 15,037 Jan-29-2019, 02:32 PM
Last Post: snippsat
  [Errno11004] Get addrinfo failed with urlopen prashanth0988 2 13,815 Aug-02-2018, 01:41 PM
Last Post: iiooii
  urllib request urlopen? nutgut 4 5,500 Apr-14-2018, 01:12 PM
Last Post: nutgut
  urllib urlopen getting error 400 on 1 specific page glidecode 4 4,119 Mar-01-2018, 11:01 PM
Last Post: glidecode

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020