[Patches] Re: urllib suggestion

Skip Montanaro skip@mojam.com (Skip Montanaro)
Tue, 8 Feb 2000 15:01:33 -0600


A request to the python-help mailing list by Markus Demleitner suggested
exposing URLOpener.server_version at the module level so that applications
can override it as appropriate.

The appended patch adds a __client_name__ variable to the module level which
is initialized to "Python-urllib" and renames __version__ to
__client_version__.  These variables are used to construct
URLopener.server_version.  I renamed __version__ to __client_version__ to
make it clear that the version number is sent from the client to the server,
is not there simply to document the version of urllib.py.  To affect the
headers sent as a result of a call to urlopen or urlretrieve, they must be
set before either function is called.

An alternative is to document that the undocumented FancyURLopener class can
be subclassed to override server_version and this class then used as the
value of urllib._urlopener for use by urlopen and urlretrieve.  I'm not
certain which alternative is better.  Both mechanisms require the programmer
to read the code.  I'll let someone else argue the pros and cons of the two
approaches (though URLopener and FancyURLopener should probably be
documented).

A minor nit: I also changed the triple-quoted module doc string to use '''
instead of """ as the string delimiter.  This helps Emacs' python-mode
behave better when colorizing the file's contents since the doc string
contains " characters.  Keep or toss.

Skip Montanaro | http://www.mojam.com/
skip@mojam.com | http://www.musi-cal.com/
847-971-7098
"Languages that change by catering to the tastes of non-users tend
not to do so well." - Doug Landauer

Release info:

I confirm that, to the best of my knowledge and belief, this contribution is
free of any claims of third parties under copyright, patent or other rights
or interests ("claims").  To the extent that I have any such claims, I
hereby grant to CNRI a nonexclusive, irrevocable, royalty-free, worldwide
license to reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part of the
Python software and its related documentation, or any derivative versions
thereof, at no cost to CNRI or its licensed users, and to authorize others
to do so.

I acknowledge that CNRI may, at its sole discretion, decide whether or not
to incorporate this contribution in the Python software and its related
documentation.  I further grant CNRI permission to use my name and other
identifying information provided to CNRI by me for use in connection with
the Python software and its related documentation.

*** /tmp/urllib.py.~1.91~	Tue Feb  8 14:45:13 2000
--- /tmp/urllib.py	Tue Feb  8 14:45:13 2000
***************
*** 1,4 ****
! """Open an arbitrary URL.
  
  See the following document for more info on URLs:
  "Names and Addresses, URIs, URLs, URNs, URCs", at
--- 1,4 ----
! '''Open an arbitrary URL.
  
  See the following document for more info on URLs:
  "Names and Addresses, URIs, URLs, URNs, URCs", at
***************
*** 20,26 ****
  The info() method returns a mimetools.Message object which can be
  used to query various info about the object, if available.
  (mimetools.Message objects are queried with the getheader() method.)
! """
  
  import string
  import socket
--- 20,26 ----
  The info() method returns a mimetools.Message object which can be
  used to query various info about the object, if available.
  (mimetools.Message objects are queried with the getheader() method.)
! '''
  
  import string
  import socket
***************
*** 28,34 ****
  import sys
  
  
! __version__ = '1.12'    # XXX This version is not always updated :-(
  
  MAXFTPCACHE = 10        # Trim the ftp cache beyond this size
  
--- 28,37 ----
  import sys
  
  
! # applications can override the following two variables to affect the
! # User-agent header that is sent to the client
! __client_version__ = '1.12'    # XXX This version is not always updated :-(
! __client_name__ = 'Python-urllib'
  
  MAXFTPCACHE = 10        # Trim the ftp cache beyond this size
  
***************
*** 89,95 ****
          self.proxies = proxies
          self.key_file = x509.get('key_file')
          self.cert_file = x509.get('cert_file')
!         server_version = "Python-urllib/%s" % __version__
          self.addheaders = [('User-agent', server_version)]
          self.__tempfiles = []
          self.__unlink = os.unlink # See cleanup()
--- 92,98 ----
          self.proxies = proxies
          self.key_file = x509.get('key_file')
          self.cert_file = x509.get('cert_file')
!         server_version = "%s/%s" % (__client_name__, __client_version__)
          self.addheaders = [('User-agent', server_version)]
          self.__tempfiles = []
          self.__unlink = os.unlink # See cleanup()