473,696 Members | 1,886 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 AuthenticatedUR L(urllib.FancyU RLopener) :
def prompt_user_pas swd(this, host, realm):
return("", "foobar")

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

urllib2:
auth_handler = urllib2.HTTPBas icAuthHandler()
auth_handler.ad d_password('Art ikelen', 'localhost:8181 ', '', 'foobar')
opener = urllib2.build_o pener(auth_hand ler) 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&rep ly_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.urlencod e(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_defa ult
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.urlencod e(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(ur l, 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(ht tplib.HTTP, req)
File "/usr/lib/python2.3/urllib2.py", line 895, in do_open
return self.parent.err or('http', req, fp, code, msg, hdrs)
File "/usr/lib/python2.3/urllib2.py", line 346, in error
result = self._call_chai n(*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(hos t, req, realm)
File "/usr/lib/python2.3/urllib2.py", line 648, in retry_http_basi c_auth
return self.parent.ope n(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(ht tplib.HTTP, req)
File "/usr/lib/python2.3/urllib2.py", line 895, in do_open
return self.parent.err or('http', req, fp, code, msg, hdrs)
File "/usr/lib/python2.3/urllib2.py", line 352, in error
return self._call_chai n(*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_defa ult
raise HTTPError(req.g et_full_url(), code, msg, hdrs, fp)
urllib2.HTTPErr or: 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 3589

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

Similar topics

5
23990
by: franck | last post by:
HI, this is my code params = {} params = '*****'; params = '***'; params = '**********'; params = '******'; params = '****';
1
6964
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 zip4query():
7
2975
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 web), but I cannot access using an automatic procedure (that is what I need to make a daemon that downloads weekly an ASCII file from that site). I've tried using urllib:
2
14046
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 accepts and processes that data. I'm not seeing how to get at data that's not a named form parameter. I wrote a simple CGI program to echo a string representation of the cgi.FieldStorage class that's recevied, and one to make an HTTP POST to it...
2
1787
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. However, when I POST, I do get a response back, but it is just the main page finance.yahoo.com and it doesn't seem as if the POST got through. I am using Python 2.2. I get a status and reason of 200 and OK respectively. I've searched through these...
11
4801
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 title='Off'><input type='hidden' value='Image' name='Action'><input type='hidden' value='hs.ExecX10ByName "Kitchen Espresso Machine", "Off", 100'></form> I've been trying this but I get a syntax error... params = urllib.urlencode({'hidden':...
6
3444
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 least for me anyway) ;) I have tried a few different sources, but I cannot get HTTP POST to successfully log in. I can login using FireFox at
4
4659
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 to be limited to GET requests. (I could be wrong though; please correct me if this is so.) TIA! kynn
4
3806
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'> <input type='hidden' name='action' value='grant-revoke' /> <input type='hidden' name='creator_badge_index' value='1' /> <input type='hidden' name='token' value='92dcd92a8bc16f73f330d118ae1ed891' /> <input type='hidden' name='do-grant'...
0
8667
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9012
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8853
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7708
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6515
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4357
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3034
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1992
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.