473,320 Members | 1,799 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,320 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 2860
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.