471,595 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Error using httplib documentation example code

Hi.

I'm having some problems with code based directly on the following
httplib documentation code:
http://www.zvon.org/other/python/doc...-examples.html

I've included the code[1] and traceback[2] at the end of this post.

The odd thing is, using DEPRECATED FUNCTIONS to perform the same
function works fine!

The working code is based on the Python 2.1.1 documentation example code
here: http://www.zvon.org/other/python/doc...-examples.html

The working code[3] and partial result[4] are at the end of this post.

What sems to be the problem is that the read() method is getting caught
up on the MIME-version part of the response -- is it expecting to
receive the HTTP response code here?

If I paste the current httplib documentation code word-for-word (using
the example site) it works...

Thanks for any help in advance!

[1] Broken code based on CURRENT DOCUMENTATION:

import httplib, urllib
def dirSearch(text):
params = urllib.urlencode({'search_text':text,
'search_method':'long'})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Content-length": '%d' % len(params),
"Accept": "text/plain",
"Host": "cnswww.cns.cwru.edu"}

h = httplib.HTTPConnection("cnswww.cns.cwru.edu")
h.request("POST", "/phone/phonebook/local/whois.cgi", headers=headers)
h.send(params)
r = h.getresponse()
print r.status, r.reason
d = r.read()
print d
h.close()

################################################## #############################
[2] Traceback from above code:
dirSearch('beck')

200 OK

Traceback (most recent call last):
File "<pyshell#52>", line 1, in -toplevel-
dirSearch('beck')
File "G:\exogen\My Documents\Projects\Python\testdir.py", line 14, in
dirSearch
d = r.read()
File "D:\Program Files\Python23\lib\httplib.py", line 389, in read
return self._read_chunked(amt)
File "D:\Program Files\Python23\lib\httplib.py", line 426, in
_read_chunked
chunk_left = int(line, 16)
ValueError: invalid literal for int(): MIME-version: 1.0

################################################## #############################
[3] Working code based on DEPRECATED FUNCTIONS:

import httplib, urllib
def dirSearch(text):
params = urllib.urlencode({'search_text': '%s' % text,
'search_method': 'long'})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Content-length": '%d' % len(params),
"Accept": "text/plain",
"Host": "cnswww.cns.cwru.edu"}

h = httplib.HTTP("cnswww.cns.cwru.edu")
h.putrequest("POST", "/phone/phonebook/local/whois.cgi")
for key in headers:
h.putheader(key, headers[key])
h.endheaders()
h.send(params)
reply, msg, hdrs = h.getreply()
print reply
d = h.getfile().read()
print d

################################################## #############################
[4] Partial result from above code:

MIME-version: 1.0

<HTML>
<HEAD>
<TITLE>CWRU Online Phonebook</TITLE>
</HEAD>
<BODY>
--
Brian Beck
Adventurer of the First Order
Jul 18 '05 #1
1 2762
Some new developments...

Using the new code, I can successfully read the contents of the response
if I use r.fp.read() instead of just r.read(), HOWEVER odd ASCII appears
in the result[1]:

################################################## #
[1] Partial result using r.fp.read():

MIME-version: 1.0

0042

<HTML>
<HEAD>
<TITLE>CWRU Online Phonebook</TITLE>
</HEAD>
<BODY>
0060

--
Brian Beck
Adventurer of the First Order
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Anand Pillai | last post: by
2 posts views Thread by John F Dutcher | last post: by
2 posts views Thread by Fuzzyman | last post: by
3 posts views Thread by alastair | last post: by
reply views Thread by Robert | last post: by
1 post views Thread by Michele Simionato | last post: by
6 posts views Thread by Peter Frost | last post: by
reply views Thread by Jaap Spies | last post: by
4 posts views Thread by kgrafals | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

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.