473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

POST with basic auth and cookie from python?


If I wanted to write a python script that performs basic auth, gets a
cookie, and then does an http POST using the cookie for authentication,
what would be the best python API to write to?

Does someone already have example code that does something like this?

Thanks.

Jul 18 '05 #1
3 3476
Dan Stromberg <st******@dcs.nac.uci.edu> wrote in message news:<pa****************************@dcs.nac.uci.e du>...
If I wanted to write a python script that performs basic auth, gets a
cookie, and then does an http POST using the cookie for authentication,
what would be the best python API to write to?

Does someone already have example code that does something like this?

Thanks.


Well... I know *part* of the solution ! I'm also struggling with http
authentiction from python. The cookie part is easy, ClientCookie (or
cookielib if you use Python 2.4) will automatically handle the cookies
for you.See http://aspn.activestate.com/ASPN/Coo.../Recipe/302930
for details of how to do that part.

I have also sussed out how to do basic authentication the first time
round (get an error code 401 from the server, repeat the request with
the username and password encoded into the header).

My test CGI at http://aspn.activestate.com/ASPN/Coo.../Recipe/298336
has an example of how to do this.

req = reqfun(theurl, None, {'User-agent' : 'Mozilla/4.0 (compatible;
MSIE 5.5; Windows NT)'})
if data['name'] and data['pass']:
import base64
base64string = base64.encodestring('%s:%s' % (data['name'],
data['pass']))[:-1]
req.add_header("Authorization", "Basic %s" % base64string)

(Which I took from another cookbook recipe I think).

After that it gets more complicated because that username/password is
valid for a whole 'realm' - and I've really struggled finding
documentation on http realms. I have the added problem that I'm doing
this all in a CGI - so any auth handlers I create aren't persistent
across consecutive page accesses - so I may need to save this
password/realm information between page accesses... yuck.

You might not have that problem and may be able to install an auth
handler with the realm information as part of your opener. If it's
really *just* the cookie providing authentication though you shouldn't
have a problem. In the meantime I've got a book on http on order from
amazon..........

Oh - if anyone can help on this it would be great !! What the heck is
a realm and how do they work !!
Regards,
Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html
Jul 18 '05 #2
On 2004-09-04, Michael Foord <fu******@gmail.com> wrote:
Dan Stromberg <st******@dcs.nac.uci.edu> wrote in message news:<pa****************************@dcs.nac.uci.e du>...
If I wanted to write a python script that performs basic auth, gets a
cookie, and then does an http POST using the cookie for authentication,
what would be the best python API to write to?

Does someone already have example code that does something like this?
Oh - if anyone can help on this it would be great !! What the heck is
a realm and how do they work !!

Not sure if this helps ...
http://aspn.activestate.com/ASPN/Coo.../Recipe/267197

Jul 18 '05 #3
Lee Harr <mi*****@frontiernet.net> wrote in message news:<sl*******************@homer.localdomain>...
On 2004-09-04, Michael Foord <fu******@gmail.com> wrote:
Dan Stromberg <st******@dcs.nac.uci.edu> wrote in message news:<pa****************************@dcs.nac.uci.e du>...
If I wanted to write a python script that performs basic auth, gets a
cookie, and then does an http POST using the cookie for authentication,
what would be the best python API to write to?

Does someone already have example code that does something like this?

Oh - if anyone can help on this it would be great !! What the heck is
a realm and how do they work !!

Not sure if this helps ...
http://aspn.activestate.com/ASPN/Coo.../Recipe/267197


Thanks - it was the comments from that recipe that I got the code to
do basic athorization. One of the commenters also states that using
urllib2.HTTPPasswordMgrWithDefaultRealm will also do the job
(remembering the username and password for the realm, once you have
supplied it). The trouble is that in *my* code (sorry to the original
poster for hijacking the thread) is a CGI. This means that for each
seperate page access the process starts afresh - so I would need to
save in an external file the username and password combinations...
which is by no means an ideal solution.

The bottom line for me is that I don't actually understand what a
realm is and how http does authentication beyond the first page access
- does it need the username and password encoded in the headers for
access to every page in that realm ?

Tell you what - I'll start another thread and see if anyone
replies.....

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jsWalter | last post by:
I have an extension Class to Auth and I'm looking for some folks to hammer on it a bit and give feed back. Class: AuthUser - add user (well, Auth does that now, so its gone) - remove user...
7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
5
by: Tony Pino | last post by:
Hi, Lets say there's a web site with simple authentication. It asks you to type a uname/password into a couple text boxes, and then it gives you a cookie and you're logged in for 20 minutes or...
4
by: 23s | last post by:
I had this problem in the past, after a server reformat it went away, and now after another server reformat it's back again - no clue what's doing it. Here's the flow: Website root is public, no...
6
by: test | last post by:
Hi everyone, I'm creating a desktop Python application that requires web-based authentication for accessing additional application features. HTTP GET is really simple. HTTP POST is not (at...
8
by: =?Utf-8?B?TFc=?= | last post by:
Hello! I am just learning about forms authentication so please excuse this basic question. I am using .NET 1.1 and C#. I have created my web.config file and my login.aspx and the associated cs...
3
by: Marco Aloisio | last post by:
Hi, I'm a Python newbie; I have to write a simple webserver, and I need to implement a basic authentication as specified in the RFC2617. I wonder if there is a Python library for doing that. ...
1
by: Max Erickson | last post by:
On Apr 13, 2:11 pm, Michel Bouwmans <mfb.chikaz...@gmail.comwrote: I've attempted to use a password manager and auth manager and failed in the past, so this is only a guess, but presumably,...
3
by: Jag | last post by:
Hi I am facing a strange issue. I have 3 ASP pages in the default website 1. auth.aspx <html> <body>
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.