473,396 Members | 2,076 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,396 software developers and data experts.

Extending and altering httplib to handle bad servers

In the course of my current project, I've had to deal with connecting
to an HTTP server that isn't fully compliant with the HTTP requirements
for chunked encodings. Rather than sending the end-of-data sentinel
(0-length chunk), it just closes the connection (without even sending
the CRLF at the end of the data).

Because of this, using httplib would always throw nasty errors and not
give me any useful data. Therefore, I've had to modify the httplib.py
code to do something reasonable when the server just closes the
connection.

So, my questions are (my changes follow, against Python 2.3):

- Did I patch the right place to do Something Reasonable in this case
of server non-compliance?

- Is there a better way to handle this case that may be more robust? Or
handle more similar cases?

- Is there anything special I should do (besides obviously diff-ing
against CVS) before submitting a patch for this to SourceForge? (it
seems to me that being tolerant of bad servers is something that would
be of general interest.)

Thanks,
Michael

---8<------- BEGIN CONTEXT DIFF ------------
*** /usr/lib/python2.3/httplib.py 2005-05-04 02:08:57.000000000 -0500
--- httplib.py 2005-08-05 10:33:08.000000000 -0500
***************
*** 1,5 ****
--- 1,7 ----
"""HTTP/1.1 client library

+ Copyright (c) 2001 Python Software Foundation; All Rights Reserved
+
<intro stuff goes here>
<other stuff, too>

***************
*** 64,69 ****
--- 66,75 ----
Unread-response _CS_IDLE <response_class>
Req-started-unread-response _CS_REQ_STARTED <response_class>
Req-sent-unread-response _CS_REQ_SENT <response_class>
+
+ Modified 2005-07-20 by Michael Ekstrand <me******@iastate.edu> to deal
+ gracefully wtih non-compliant systems which just terminate the connection
+ rather than sending the end-of-data chunk in chunked HTTP responses.
"""

import errno
***************
*** 442,448 ****
amt -= chunk_left

# we read the whole chunk, get another
! self._safe_read(2) # toss the CRLF at the end of the chunk
chunk_left = None

# read and discard trailer up to the CRLF terminator
--- 448,460 ----
amt -= chunk_left

# we read the whole chunk, get another
! try:
! self._safe_read(2) # toss the CRLF at the end of the chunk
! except IncompleteRead:
! # The server just closed on us, without providing appropriate
! # end-of-data things.
! self.close()
! return value
chunk_left = None

# read and discard trailer up to the CRLF terminator
---8<--------- END CONTEXT DIFF -------------
Aug 8 '05 #1
0 1071

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rich | last post by:
Hi all, I'm trying to write an app to monitor an IIS server, by connecting and getting a page via httplib. the problem seems to be when IIS (or ASP) dies httplib does not always return and...
0
by: Shagshag | last post by:
hello, how can i set multiple cookies like : Set-Cookie: cookiename1=value1; expires=Tue, 31-Mar-09 11:17:43 GMT; path=/; domain=www.domain.com Set-Cookie: cookiename2=value2; expires=Tue,...
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...
1
by: Brian Beck | last post by:
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 and...
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;...
4
by: Patrick Altman | last post by:
I am attempting to use a HEAD request against Amazon S3 to check whether a file exists or not and if it does parse the md5 hash from the ETag in the response to verify the contents of the file so...
3
by: rhXX | last post by:
hi all, i'm using this tutorial example import httplib h = httplib.HTTP("www.python.org") h.putrequest('GET','/index.html') h.putheader('User-Agent','Lame Tutorial Code')...
3
by: Henrik Lied | last post by:
Hi there! I'm using a recipe found on ASPN to upload some data to an external server. The request fails, and all I get in response is "No backend servers available". So I'm wondering: Is...
0
by: reacocard | last post by:
Hi, I'm writing a download manager in python, and httplib is being very slow when pulling from localhost or even other servers on the local network. I'm getting about 10MB in 14s with httplib,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...

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.