Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7312

urllib2 opendirector versus request object

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <daodennis@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'handler': 0.05; 'python:': 0.05; 'sys': 0.05; 'initialize': 0.07; 'server:': 0.07; 'urllib2': 0.07; "'http/1.1": 0.09; 'base64': 0.09; 'handlers': 0.09; 'header,': 0.09; 'httplib': 0.09; 'received:mail- bw0-f46.google.com': 0.09; 'skip:{ 20': 0.09; 'subject:object': 0.09; 'examples': 0.12; 'debugging': 0.14; "'post": 0.16; 'connection:': 0.16; 'header:': 0.16; 'instantiate': 0.16; 'mozilla/5.0': 0.16; 'object?': 0.16; 'received:209.85.214.46': 0.16; 'res': 0.16; 'res.read()': 0.16; 'send:': 0.16; 'subject:versus': 0.16; 'url:diveintopython': 0.16; 'skip:[ 10': 0.26; 'not.': 0.26; 'windows': 0.26; 'object': 0.26; 'wondering': 0.28; 'message-id:@mail.gmail.com': 0.28; 'received:209.85.214': 0.28; 'import': 0.29; 'host': 0.29; 'url:non-standard http port': 0.30; 'hi,': 0.31; 'print': 0.31; "skip:' 10": 0.32; 'adds': 0.32; 'headers': 0.32; 'does': 0.33; 'to:addr:python-list': 0.33; 'list': 0.33; 'skip:# 10': 0.34; 'using': 0.35; 'open': 0.36; 'difference': 0.37; 'received:google.com': 0.37; 'issue': 0.37; 'received:209.85': 0.37; 'http': 0.37; 'response': 0.37; 'two': 0.37; 'url:org': 0.38; 'but': 0.38; 'received:209': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'here': 0.66; 'username': 0.73; 'advantages': 0.77; 'dennis': 0.77; 'opener': 0.84; 'director,': 0.97
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=BDOaS9zgMFilZQ8SyYOEijbqjRnDbCML2LveFEyqT2A=; b=Jr3x4sxy/ACUXe7+t5OK/l726rTf2qTsO3e2A4qBqSBWx2XsBzD1eudgQOE7P9RoV4 pY+Uh5cMAuSSC36Tt1QTuNFJIxsmwARS/XJ0ZPFDJgWlWE7SmSp9cjIPwlbHMTJMuaR4 xnknsF4qBfn4mSAGqmMcyQUeRA6006Rv9a0X4=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=fVK3BO2dtbEw3nm8AWbX8/in04zBlNcMqyP9ye+87q5prCXodXWfl2dEYB1AkryGR3 Qr93eqGgGiTevMEtH7gKuHKJXjtPaOh30NiQ1AXO/j/Sn6leZddxTjyoYGHWfUmrvUXA BYItfHe2TavO69iEK6KfcduOB++Yb5tKdCeIk=
MIME-Version 1.0
Date Thu, 9 Jun 2011 12:30:04 -0700
Subject urllib2 opendirector versus request object
From Dennis <daodennis@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.58.1307647806.11593.python-list@python.org> (permalink)
Lines 72
NNTP-Posting-Host 82.94.164.166
X-Trace 1307647807 news.xs4all.nl 49181 [::ffff:82.94.164.166]:52304
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7312

Show key headers only | View raw


Hi,

I was wondering what the difference or advantages to using an
opendirector with handlers or using a request object?

I am having an issue where when I use the open director and I try to
add headers it adds them after the connection-close header, but when I
use the request object it does not.

Here is the headers as reported by python:
send: 'POST /xml-api/listaccts HTTP/1.1\r\nAccept-Encoding:
identity\r\nContent-Length: 13\r\nHost:
cpanel01.sea.fibercloud.com:2086\r\nContent-Type:
application/x-www-form-urlencoded\r\nConnection: close\r\nUser-Agent:
Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv,1.9.2.13)
Gecko/20101203 Firefox/3.6.13\r\n\r\n'
send: 'domain=anjopa'
reply: 'HTTP/1.1 403 Forbidden\r\n'
header: Connection: close
header: Server: cpsrvd/11.30.0.27
header: Content-type: text/xml

Next two examples one with Request object and the next with the open director,

#!/usr/bin/python
import sys
from xml.dom.minidom import parse, parseString
import urllib
import urllib2
import base64
from cookielib import CookieJar

# Turn on HTTP debugging
http://diveintopython.org/http_web_services/user_agent.html
import httplib



#With Request object:

  req = urllib2.Request(url, {},{'Authorization':'Basic ' +
base64.b64encode( username + ':' + password ) } )
  res = urllib2.urlopen(req)
  print res.read()

With open director:
  # Instantiate and Initialize AuthInfo Handler for use w/ the build_opener

    authinfo = urllib2.HTTPBasicAuthHandler()
    authinfo.add_password(realm="Web Host Manager",
                        uri="http://servername:2086/xml-api/listacct",
                        user="username",
                        passwd="password")

    # Instantiate Cookie jar Handler for use w/ build_opener

    cj = CookieJar()

    # Create an opener object from list of handlers above
    opener = urllib2.build_opener(authinfo,urllib2.HTTPCookieProcessor(cj),
urllib2.HTTPHandler(debuglevel=1))
    urllib2.install_opener(opener)

    opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows; U;
Windows NT 6.1; en-US; rv,1.9.2.13) Gecko/20101203 Firefox/3.6.13')]
    response = opener.open(url, paramaters)





Dennis O.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

urllib2 opendirector versus request object Dennis <daodennis@gmail.com> - 2011-06-09 12:30 -0700

csiph-web