Error using httplib documentation example code

Brian Beck exogen at gmail.com
Fri Oct 8 19:56:19 EDT 2004


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[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/doc21/lib/httplib-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



More information about the Python-list mailing list