Passing a Cookie with httplib

Discussion in 'Python' started by scott@bogusaddress.com, Jun 28, 2006.

  1. Guest

    Hello,

    From a shell script, I have used /usr/bin/curl to access a web site
    and pass a cookie (as required by the site). But, I can't seem to
    accomplish this task with Python. I would like to use the httplib
    module to do this. Any thoughts on this subject? I would like to
    hard code the cookie in the code so it works every time:

    i.e. cookie = 'auth=buster%3A12345678901234567890exZ9rzMqgtxa5A'.

    The question is - how do I pass it with httplib? Here is what I have
    so far:


    # Import external modules
    import sys
    import httplib

    # Define our site and path
    WP_SERVER="www.foo.com"
    WP_PATH='/somedir/confirmquery?value=%s'
    try:
    # Grab the command line argument
    uservalue = sys.argv[1]
    # Use the httplib module and connect
    conn=httplib.HTTPConnection(WP_SERVER)
    conn.request('GET',WP_PATH % uservalue)
    response=conn.getresponse()
    data=response.read()
    conn.close()
    print data
    except:
    print "Some stupid error occurred"
    sys.exit(1)


    Any help would be greatly appreciated,

    Scott
    , Jun 28, 2006
    #1
    1. Advertising

  2. On 2006-06-28, <> wrote:

    > From a shell script, I have used /usr/bin/curl to access a web site
    > and pass a cookie


    I use ClientCookie for that.

    http://wwwsearch.sourceforge.net/ClientCookie/

    --
    Grant Edwards grante Yow! I'm into SOFTWARE!
    at
    visi.com
    Grant Edwards, Jun 28, 2006
    #2
    1. Advertising

  3. In article <>,
    wrote:

    >I would like to
    >hard code the cookie in the code so it works every time:
    >
    >i.e. cookie = 'auth=buster%3A12345678901234567890exZ9rzMqgtxa5A'.
    >
    > conn=httplib.HTTPConnection(WP_SERVER)
    > conn.request('GET',WP_PATH % uservalue)


    According to <http://docs.python.org/lib/httpconnection-objects.html>,
    you can pass additional "body" and "headers" args to
    HTTPConnection.request. How about trying something like this in place of
    the last line above:

    Headers = {"Cookie" :
    "auth=buster%3A12345678901234567890exZ9rzMqgtxa5A"}
    conn.request('GET',WP_PATH % uservalue, None, Headers)
    Lawrence D'Oliveiro, Jun 29, 2006
    #3
  4. Guest

    On Thu, 29 Jun 2006 21:42:50 +1200, Lawrence D'Oliveiro
    <_zealand> wrote:

    >According to <http://docs.python.org/lib/httpconnection-objects.html>,
    >you can pass additional "body" and "headers" args to
    >HTTPConnection.request. How about trying something like this in place of
    >the last line above:
    >
    > Headers = {"Cookie" :
    >"auth=buster%3A12345678901234567890exZ9rzMqgtxa5A"}
    > conn.request('GET',WP_PATH % uservalue, None, Headers)


    Perfect! Thanks so much. I appreciate your help. I looked through
    the docs before, but didn't spot this nugget because I was focused on
    searching for the text "cookie". Obviously, it pays to read... :)

    Scott
    , Jun 29, 2006
    #4
  5. Guest

    On Wed, 28 Jun 2006 20:21:37 -0000, Grant Edwards <>
    wrote:

    >On 2006-06-28, <> wrote:
    >
    >> From a shell script, I have used /usr/bin/curl to access a web site
    >> and pass a cookie

    >
    >I use ClientCookie for that.
    >
    >http://wwwsearch.sourceforge.net/ClientCookie/


    Thanks Grant. I'll take a look at this solution.

    Scott
    , Jun 29, 2006
    #5
  6. John J. Lee Guest

    Grant Edwards <> writes:

    > On 2006-06-28, <> wrote:
    >
    > > From a shell script, I have used /usr/bin/curl to access a web site
    > > and pass a cookie

    >
    > I use ClientCookie for that.
    >
    > http://wwwsearch.sourceforge.net/ClientCookie/


    Note that ClientCookie has moved, to become part of mechanize (well,
    is moving -- mechanize is still in beta):

    > http://wwwsearch.sourceforge.net/ClientCookie/



    mechanize exports a superset of the ClientCookie interface, so "import
    mechanize as ClientCookie" should be all you need to do to switch
    (modulo some trivial details, documented at the URL below).

    Also note that module cookielib in the Python 2.4 stdlib contains most
    of the functionality of ClientCookie (specifically, all the cookie
    handling code, of course).


    John
    John J. Lee, Jun 29, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    3
    Views:
    5,837
    Steven Cheng[MSFT]
    Jun 3, 2004
  2. Shapper

    Cookie and Session Cookie Questions.

    Shapper, Apr 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    565
  3. =?Utf-8?B?UGF1bA==?=

    Cookie Question (IP as domain and cookie file location)

    =?Utf-8?B?UGF1bA==?=, Jan 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,473
    Bruce Barker
    Jan 10, 2006
  4. ad
    Replies:
    2
    Views:
    7,506
    Kevin Spencer
    Jan 27, 2006
  5. =?Utf-8?B?TnVubw==?=

    Convert a PHP cookie to an ASP.NET cookie

    =?Utf-8?B?TnVubw==?=, Jan 31, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    438
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 31, 2006
Loading...

Share This Page