Detecting Browsers in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Orner

    Detecting Browsers in Python

    Does anyone know of a simple way to have a Python script find out what
    browser is accessing it? After a web search the only thing I found to
    do this is Zope, but the system I'm programming doesn't use Zope and
    I'm not really interested in installing it just for this minor detail.
    Is there another way?

    Thanks!

    --Daniel Orner
  • Jay O'Connor

    #2
    Re: Detecting Browsers in Python

    Daniel Orner wrote:
    [color=blue]
    >Does anyone know of a simple way to have a Python script find out what
    >browser is accessing it? After a web search the only thing I found to
    >do this is Zope, but the system I'm programming doesn't use Zope and
    >I'm not really interested in installing it just for this minor detail.
    >Is there another way?
    >
    >Thanks!
    >
    >--Daniel Orner
    >
    >[/color]

    Going purely from memory, "USER-AGENT" or something similar entry in the
    os.environ dictionary will have a string identifying the browser. It's
    only partially reliable because some browsers will pretend to be another
    browser and some, I think, will allow the user to specify what they want
    to pretend to be.

    Anyway, play around with it and see if will give you what you need

    Take care,
    Jay

    Comment

    • Gerhard Häring

      #3
      Re: Detecting Browsers in Python

      Daniel Orner wrote:[color=blue]
      > Does anyone know of a simple way to have a Python script find out what
      > browser is accessing it? After a web search the only thing I found to
      > do this is Zope, but the system I'm programming doesn't use Zope and
      > I'm not really interested in installing it just for this minor detail.
      > Is there another way?[/color]

      Interpret the User-Agent field.

      It depends on your system how to acess the User-Agent field. If you're
      using a CGI script, try this one:

      import os
      print "Content-type: text/plain"
      print
      print "User-Agent:", os.environ.get( "HTTP_USER_AGEN T", "N/A")

      To see what information you can access from a web request, try this CGI
      script:

      import cgi
      cgi.test()

      For actually interpreting this field, a Google search for "python parse
      HTTP_USER_AGENT " would be my starting point.

      HTH,

      -- Gerhard

      PS: In case you're using Apache, you can use modules like mod_rewrite to
      redirect users to specialized pages, depending on the reported User-Agent.


      Comment

      • Fredrik Lundh

        #4
        Re: Detecting Browsers in Python

        Daniel Orner wrote:
        [color=blue]
        > Does anyone know of a simple way to have a Python script find out what
        > browser is accessing it? After a web search the only thing I found to
        > do this is Zope, but the system I'm programming doesn't use Zope and
        > I'm not really interested in installing it just for this minor detail.
        > Is there another way?[/color]

        if you're using a CGI script, you can (usually) find the user agent
        in the HTTP_USER_AGENT environment variable; use os.environ to
        get the current value.

        #!/usr/bin/python -u

        import os

        print "Content-type: text/plain"
        print

        print "User agent:", os.environ.get( "HTTP_USER_AGEN T", "unknown")

        if you're using some other framework, you may be able to get the
        user agent by looking at the HTTP headers; check the framework
        docs for details.

        for more info on CGI environment variables, see:




        (etc)

        </F>




        Comment

        • Dave Brueck

          #5
          Re: Detecting Browsers in Python

          > Does anyone know of a simple way to have a Python script find out what[color=blue]
          > browser is accessing it? After a web search the only thing I found to
          > do this is Zope, but the system I'm programming doesn't use Zope and
          > I'm not really interested in installing it just for this minor detail.
          > Is there another way?[/color]

          (I assume you mean that the script the browser is accessing is a CGI script)

          Most browsers include a "User-Agent" in the HTTP request they make to a server.
          Users can override these, but few people do, so you can semi-reliably detect
          the browser that way.

          I sometimes need to make sure a browser is running on "new enough" Windows, so
          I use this:

          ua = ua.lower()

          if ua.find('win') != -1 and ua.find('win16' ) == -1 and \
          ua.find('window s nt 4') == -1 and ua.find('winnt4 ') == -1:
          # platform is Windows
          else:
          # non-Windows or old Windows

          As for browser vendor, this is the pseudocode I use:

          if ua.find('opera' ) != -1:
          # opera
          elif ua.find('gecko' ) != -1:
          # gecko (moz/ns)
          elif ua.find('msie') != -1:
          # Most likely really is IE
          else:
          # somebody else

          This works for what I need because usually I'm just trying to acertain if the
          browser is IE on a newer Windows box, but you may need additional checks
          (Google can turn up huge lists of all known default User-Agent strings so it's
          fairly easy to come up with a good test suite).

          -Dave



          Comment

          Working...