473,507 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

POST data with 401 authentication using urllib(2)

Hi all,

I'm trying to submit some data using a POST request to a HTTP server with
BASIC authentication with python, but I can't get it to work. Since it's
driving me completely nuts, so here's my cry for help.

The server is an elog logbook server (http://midas.psi.ch/elog/). It is
protected with a password and an empty username. I can login both using
urllib and urllib2 (suppose the password is "foobar", the logbook is
running on port 8181, and I need the directory "Artikelen")

urllib:
class AuthenticatedURL(urllib.FancyURLopener) :
def prompt_user_passwd(this, host, realm):
return("", "foobar")

url = AuthenticatedURL()
data = ""
f = url.open("http://localhost:8181/Artikelen")

urllib2:
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password('Artikelen', 'localhost:8181', '', 'foobar')
opener = urllib2.build_opener(auth_handler) urllib2.install_opener(opener)
f = urllib2.urlopen('http://localhost:8181/Artikelen')

So far so good. But I want to submit some data via CGI. I can do this by
making a GET request out of the URL:
f = url.open("http://localhost:8181/Artikelen/?cmd=Submit&reply_to=80")
This works as advertised (also with urllib2).

However, since I also need to upload files, I want to do this via a POST
request (as it is donenormally via the web interface). However, if I try
this using either urllib or urllib2, it throws an exception:

data = {"cmd" : "Submit", "reply_to" : "80"}

urllib:
f = url.open("http://localhost:8181/Artikelen/", urllib.urlencode(data))
Traceback (most recent call last):
File "login.py", line 12, in ?
f = url.open("http://pde.dyndns.org:8181", data)
File "/usr/lib/python2.3/urllib.py", line 183, in open
return getattr(self, name)(url, data)
File "/usr/lib/python2.3/urllib.py", line 308, in open_http
return self.http_error(url, fp, errcode, errmsg, headers, data)
File "/usr/lib/python2.3/urllib.py", line 323, in http_error
return self.http_error_default(url, fp, errcode, errmsg, headers)
File "/usr/lib/python2.3/urllib.py", line 551, in http_error_default
return addinfourl(fp, headers, "http:" + url)
File "/usr/lib/python2.3/urllib.py", line 837, in __init__
addbase.__init__(self, fp)
File "/usr/lib/python2.3/urllib.py", line 787, in __init__
self.read = self.fp.read
AttributeError: 'NoneType' object has no attribute 'read'

urllib2:
f = urllib2.urlopen('http://localhost:8181/Artikelen/',
urllib.urlencode(data))
Traceback (most recent call last):
File "login2.py", line 11, in ?
f = urllib2.urlopen('http://localhost:8181/Artikelen/', data)
File "/usr/lib/python2.3/urllib2.py", line 129, in urlopen
return _opener.open(url, data)
File "/usr/lib/python2.3/urllib2.py", line 326, in open
'_open', req)
File "/usr/lib/python2.3/urllib2.py", line 306, in _call_chain
result = func(*args)
File "/usr/lib/python2.3/urllib2.py", line 901, in http_open
return self.do_open(httplib.HTTP, req)
File "/usr/lib/python2.3/urllib2.py", line 895, in do_open
return self.parent.error('http', req, fp, code, msg, hdrs)
File "/usr/lib/python2.3/urllib2.py", line 346, in error
result = self._call_chain(*args)
File "/usr/lib/python2.3/urllib2.py", line 306, in _call_chain
result = func(*args)
File "/usr/lib/python2.3/urllib2.py", line 659, in http_error_401
host, req, headers)
File "/usr/lib/python2.3/urllib2.py", line 638, in http_error_auth_reqed
return self.retry_http_basic_auth(host, req, realm)
File "/usr/lib/python2.3/urllib2.py", line 648, in retry_http_basic_auth
return self.parent.open(req)
File "/usr/lib/python2.3/urllib2.py", line 326, in open
'_open', req)
File "/usr/lib/python2.3/urllib2.py", line 306, in _call_chain
result = func(*args)
File "/usr/lib/python2.3/urllib2.py", line 901, in http_open
return self.do_open(httplib.HTTP, req)
File "/usr/lib/python2.3/urllib2.py", line 895, in do_open
return self.parent.error('http', req, fp, code, msg, hdrs)
File "/usr/lib/python2.3/urllib2.py", line 352, in error
return self._call_chain(*args)
File "/usr/lib/python2.3/urllib2.py", line 306, in _call_chain
result = func(*args)
File "/usr/lib/python2.3/urllib2.py", line 412, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error -1:

In fact, I get these exact same errors if "data" is empty. I'm
wondering if this is supported in python. I can't find anything about it
in TFM. If it's not supported, does anybody have an idea how to do it? If
it is supported, can anybody tell what I'm dowing wrong?

Any help is greatly appreciated.

Pieter Edelman
Jul 18 '05 #1
0 3566

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

Similar topics

5
23980
by: franck | last post by:
HI, this is my code params = {} params = '*****'; params = '***'; params = '**********'; params = '******'; params = '****';
1
6951
by: Steve Allgood | last post by:
I'm having trouble posting a form at the USPS web site. I've been successful using urllib at other sites, but I'm missing why this won't work: # begin code # get zip+4 import urllib def...
7
2960
by: luigipaioro | last post by:
Good morning to all! I'm trying to access on a web page that needs user and password authentication. I'm enabled to access there (I mean that I have an user name and a password to access via...
2
14002
by: Erik Johnson | last post by:
I am trying to work with a program that is trying make an HTTP POST of text data without any named form parameter. (I don't know - is that a normal thing to do?) I need to write a CGI program that...
2
1777
by: vm | last post by:
Hi, for some reason my POST is not working properly. I am basically just trying to get a simple stock quote from yahoo by posting the ticker symbol (GE as an example) into finance.yahoo.com. ...
11
4791
by: livin | last post by:
I need to post form data to an ASP page that looks like this on the page itself... <form method='POST'><input src=\icons\devices\coffee-on.gif type='image' align='absmiddle' width=16 height=16...
6
3427
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...
4
4643
by: kj | last post by:
Hi. Sorry for this very clueless question, but how does one write in Python an HTTP client that can send a POST request? The modules I've found (e.g. urllib, urllib2), as far as I can tell, seem...
4
3799
SuperMetroid
by: SuperMetroid | last post by:
The html code of the form, and my code are below. I can't get the value to post/submit.. instead I get an error. Can anyone help? HTML Code of Form: <form method='post' autocomplete='off'> ...
0
7221
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
7109
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
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7481
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...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.