473,581 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.urlencod e({'search_text ':text,
'search_method' :'long'})
headers = {"Content-type": "applicatio n/x-www-form-urlencoded",
"Content-length": '%d' % len(params),
"Accept": "text/plain",
"Host": "cnswww.cns.cwr u.edu"}

h = httplib.HTTPCon nection("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#5 2>", line 1, in -toplevel-
dirSearch('beck ')
File "G:\exogen\ My Documents\Proje cts\Python\test dir.py", line 14, in
dirSearch
d = r.read()
File "D:\Program Files\Python23\ lib\httplib.py" , line 389, in read
return self._read_chun ked(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.urlencod e({'search_text ': '%s' % text,
'search_method' : 'long'})
headers = {"Content-type": "applicatio n/x-www-form-urlencoded",
"Content-length": '%d' % len(params),
"Accept": "text/plain",
"Host": "cnswww.cns.cwr u.edu"}

h = httplib.HTTP("c nswww.cns.cwru. edu")
h.putrequest("P OST", "/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().rea d()
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 2874
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
16288
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 is an example. <TRACEBACK> Traceback (most recent call last):
2
6084
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 #80 and line # 38 are the first line offenders. --> --> -->
2
4732
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 give a more useful message to the user when the page fetch fails. My understanding was that a URLError exception had a 'code' attribute which was...
3
15432
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 transfers eventually ....). At the moment I can test out sending a set of parameters to the server, and using mod_python, I have a python script which...
0
2920
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; rv:1.6b) Gecko/20031205 Thunderbird/0.4 X-Accept-Language: en-us, en
1
4876
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
4729
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 appreciated. Thanks in advance
0
2792
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 2005, 15:19:29)
4
9336
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
7789
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
8144
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. ...
1
7894
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
8169
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...
0
6551
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5670
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
3820
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1400
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.