473,547 Members | 2,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

httplib HTTPConnection request problem

Hi,

I am having a problem with the httplib HTTPConnection object. While I
can easily send requests that don't have any payload (ie. "get"), I
encounter issues if I want to post xml data. If you look at the class
below, when req == 'getFreeBusyInf o' all functions perfectly, but when
req == 'conflictReques t' I run into problems of the nature "400 Bad
Request". As you will notice, I also tried the shorter HTTPConnection
instance method "request", feeding it a dictionary of the header
arguments and the xml payload, but that didn't fly either. With the
headers in this format, I get the error:

AttributeError: 'str' object has no attribute 'iteritems' in line 737
of httplib.py

However, this dictionary format is exactly the same as what I found in
an example in the python documentation.

Class description and example of the xml payload are as follows:

class Connector:
def __init__(self, server):
self.server = 'http://'+server
#self.h1 = httplib.HTTPCon nection('my.pro xy.org', 3128)
self.h1 = httplib.HTTPCon nection('xxx.xx x.xxx.xxx', 80)

def sendInfo(self, req, confDict, xml=None):
print xml
if req == 'getFreeBusyInf o':
self.h1.putrequ est('GET',
self.server+"/servlet/webdav.freebusy ?username="+con fDict['users']+"&server=netli ne.de&start="+s tr(confDict['request_start'])+"&end="+str(c onfDict['request_end']))
self.h1.puthead er('Accept-Language', 'de, en-us;q=0.2')
self.h1.puthead er('Translate', 'f')
self.h1.puthead er('User-Agent', 'SLOX HolidayInfo')
self.h1.puthead er('Host', self.server)
self.h1.endhead ers()
elif req == 'conflictReques t':
self.h1.putrequ est("POST",
self.server+"/servlet/webdav.calendar/conflict.xml")
self.h1.puthead er("Accept-Language", "de, en-us;q=0.2")
self.h1.puthead er("Translate" , "f")
self.h1.puthead er("User-Agent", "SLOX HolidayInfo")
self.h1.puthead er("Host", confDict['server'])
self.h1.puthead er("Content-Length", str(len(xml)))
self.h1.puthead er("Authorizati on", "Basic
"+str(confD ict['base64Auth']))
self.h1.endhead ers()
self.h1.send(xm l)
#headers = {"Accept-Language": "de, en-us;q=0.2", "Translate" : "f",
"User-Agent": "SLOX HolidayInfo", "Host": confDict['server'],
"Content-Length": str(len(xml)), "Authorization" : "Basic
"+str(confD ict['base64Auth'])}
#self.h1.reques t('POST',
self.server+"/servlet/webdav.calendar/conflict.xml", headers, xml)

def receiveInfo(sel f):
rec = self.h1.getresp onse()
return rec

def closeConnection (self):
print 'Closing connection....'
self.h1.close()
The XML data looks something like this:

<?xml version="1.0" encoding="UTF-8"?><D:multista tus
xmlns:D="DAV:"> <D:propertyupda te
xmlns:D="DAV">< D:set><D:prop>< S:begins
xmlns:S="SLOX:" >108180720000 0</S:begins><S:end s
xmlns:S="SLOX:" >108197994000 0</S:ends><S:clien tid
xmlns:S="SLOX:" >HolidyInfoID </S:clientid><S:f olderid
xmlns:S="SLOX:" ></S:folderid><S:t itle xmlns:S="SLOX:" >HolidayInfo
Conflict Request</S:title><S:appo intment_request
xmlns:S="SLOX:" >yes</S:appointment_r equest><S:membe rs
xmlns:S="SLOX:" ><S:member>xyzx yz</S:member></S:members></D:prop></D:set></D:propertyupdat e></D:multistatus>

Incidentally, I set this same thing up using sockets and everything
worked perfectly. However, I need to now use with a proxy so that is
why I moved to the httplib module. I also tried the urllib2 module
but the only request data to be sent is
application/x-www-form-urlencoded and I need to send xml.

If someone can help me out here, I would certainly appreciate it.

Thanks,

Scum
Jul 18 '05 #1
2 15915
First make sure that you urlencode the xml like the docs do:

params = urllib.urlencod e({'xml': xml})

Make sure the key is something the server is expecting. Then make sure you call request properly:

self.h1.request ('POST', self.server+"/servlet/webdav.calendar/conflict.xml",
params, headers)

Notice that params comes before headers.

Not tested but that looks like what your problem might be...

phda
On 31 Mar 2004 10:02:45 -0800
sc*****@3square s.com (scummer) wrote:
#headers = {"Accept-Language": "de, en-us;q=0.2", "Translate" : "f",
"User-Agent": "SLOX HolidayInfo", "Host": confDict['server'],
"Content-Length": str(len(xml)), "Authorization" : "Basic
"+str(confD ict['base64Auth'])}
#self.h1.reques t('POST',
self.server+"/servlet/webdav.calendar/conflict.xml", headers, xml)

Jul 18 '05 #2
scummer wrote:
Hi,

I am having a problem with the httplib HTTPConnection object. While I
can easily send requests that don't have any payload (ie. "get"), I
encounter issues if I want to post xml data. If you look at the class
below, when req == 'getFreeBusyInf o' all functions perfectly, but when
req == 'conflictReques t' I run into problems of the nature "400 Bad
Request". As you will notice, I also tried the shorter HTTPConnection
instance method "request", feeding it a dictionary of the header
arguments and the xml payload, but that didn't fly either. With the
headers in this format, I get the error:


dunno what's wrong with your code, but i recently used HTTPConnection
to transfer an XML payload to an apache + webware setup. the code below
actually works:

# --- BEGIN ---

from httplib import HTTPConnection

atomMIME = 'application/x.atom+xml'
conn = HTTPConnection( 'locahost', 80)

print '--------------------------------'
print 'Create a new entry'
print '--------------------------------'

headers = {'Content-Type': atomMIME}
postData = '''
<?xml version="1.0" encoding="iso-8859-1"?>
<entry xmlns="http://purl.org/atom/ns#">
<title>A post</title>
<created>2003-08-12T23:53:03Z</created>
<summary>An automated post</summary>
<content type="text/html" mode="escaped"> This is a test</content>
</entry>
'''.strip()

conn.request('P OST','/reflex/atom/AtomHandler', postData, headers)
res = conn.getrespons e()
assert res.status == 201 # created?

print res.status, res.reason

entryURI = res.msg['location']
print "location:" , entryURI

print '*** PASSED ***'

conn.close()

# --- END ---

hope this helps.

--
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<#me> a foaf:Person ; foaf:nick "deelan" ;
foaf:weblog <http://www.deelan.com/> .
Jul 18 '05 #3

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

Similar topics

4
3797
by: Roger Binns | last post by:
I have just spent several weeks mashing xmlrpc, httplib and SSL (from M2Crypto) to work together. The current standard library has several problems: - Builtin SSL is pretty much useless if you actually care about security - Poor HTTP authentication support - No server side stuff (SSL, HTTP authentication etc) - Pathological coding to...
1
2871
by: Brian Beck | last post by:
Hi. I'm having some problems with code based directly on the following httplib documentation code: http://www.zvon.org/other/python/doc21/lib/httplib-examples.html I've included the code and traceback at the end of this post. The odd thing is, using DEPRECATED FUNCTIONS to perform the same function works fine!
0
1466
by: Laszlo Zsolt Nagy | last post by:
Hello, This is from the docs, from section 11.6.1 (HTTPConnection Objects) HTTPConnection instances have the following methods: request( method, url]) The headers argument should be a mapping of extra HTTP headers to send with the request.
2
10753
by: spamsink42 | last post by:
this code h=httplib.HTTPConnection('euronext.com') h.request('GET', 'http://www.euronext.com/home/0,3766,1732,00.html') fails with this message File "httplib.py", line 532, in connect socket.SOCK_STREAM):
4
11787
by: JuHui | last post by:
how to use httplib.HTTPConnection with http proxy?
5
19798
by: scott | last post by:
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....
3
1924
by: Mark rainess | last post by:
The program displays images from a motion jpeg webcam. (Motion jpeg is a bastardization of multi-part mime.) <http://wp.netscape.com/assist/net_sites/pushpull.html> It runs perfectly for about 4 hours, then freezes. I'm stuck. How do I debug this? (Using: Python 2.4.3, wxPython 2.6.3.2, Windows 2000 and XP) There are no tracebacks, the...
0
1570
by: Baptiste Lepilleur | last post by:
I activated httplib debug, and when trace are printed, a UnicodeError exception is thrown. I have already set sys.stdout to use utf-8 encoding (this removed the exception when *I* was printing unicode), but from the stacktrace below, the encoding seems to magically have switched to 'ascii' when httplib does the printing... import codecs...
0
1391
by: Dustin J. Mitchell | last post by:
I'm building an interface to Amazon's S3, using httplib. It uses a single object for multiple transactions. What's happening is this: HTTP PUT /unitest-temp-1161039691 HTTP/1.1 HTTP Date: Mon, 16 Oct 2006 23:01:32 GMT HTTP Authorization: AWS <<cough>>:KiTWRuq/6aay0bI2J5DkE2TAWD0= HTTP (end headers) HTTP < HTTP/1.1 200 OK HTTP <...
0
7437
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7703
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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...
1
7463
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7797
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...
1
5362
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...
0
5081
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...
0
3493
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...
1
1050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.