473,388 Members | 894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 2862
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Anand Pillai | last post by:
Hi I am quite familiar with normal python errors which can be caught by using the try... except... finally clause. But very often I find other kinds of exceptions raised in my programs. Here...
2
by: John F Dutcher | last post by:
Can anyone comment on why the code shown in the Python error is in some way incorrect...or is there a problem with Python on my hoster's site ?? The highlites don't seem to show here...but line...
2
by: Fuzzyman | last post by:
I've written a CGI proxy that remotely fetches web pages. I use the urlopen function in CLientCookie which replaces (and ultimately uses) the urlopen function in urllib2. What I'd like to do is...
3
by: alastair | last post by:
Hi, I'm attempting to test out some functionality of the Apache http server. What I'd like to do is send a file to the server - eg. a text file or binary file (I will be testing gzipped...
0
by: Robert | last post by:
did you solve this problem? It seems to be still present here with py2.3.5. Robert -- From: Manish Jethani <manish.j@gmx.net> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US;...
1
by: Michele Simionato | last post by:
Can somebody provide an example of how to retrieve a https url, given username and password? I don't find it in the standard documentation. TIA, Michele Simionato
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
0
by: Jaap Spies | last post by:
Hi, Running Fedora Core 4: Python 2.4.3 and Python 2.4.1. I'm getting: IOError: (2, 'No such file or directory') all the time. Trying to track down this problem: Python 2.4.1 (#1, May 16...
4
by: kgrafals | last post by:
Hi, I'm just trying to read from a webpage with urllib but I'm getting IOErrors. This is my code: import urllib sock = urllib.urlopen("http://www.google.com/") and this is the error:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...

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.