Use of httplib?
David Lees
DavidL at nospamy_raqia.com
Thu Dec 14 15:55:38 EST 2000
Thanks to the kind help of Steve Holden, I have a much better solution
than the httlib one below. I used the urllib, with the code:
f = urllib.urlopen("http://192.168.0.3:50006",body)
m = f.info()
NumBytes = string.atoi(m.getheaders('content-length')[0])
BytesRead = 0
while (BytesRead < NumBytes):
data = f.readline()
BytesRead = BytesRead+len(data)
print data
f.close()
This works fine and seems easier than the http below or socket level
that I previously tried.
david
David Lees wrote:
>
> I am running the client code below to try and send and receive text from
> a server. My problem is that I do not know how to recieve back an
> arbitrary number of lines. In a test case, when I know the exact number
> of lines as shown below (e.g. 11) I can use readline. However, in
> general I do not know how many lines there are and the program hangs on
> the connection when I accidentaly read to the end. The comment out
> pieces of code also either hang or do not give back all the text.
>
> I am copying from the Library Reference section 11.4, but it does not
> seem to document the readline, read or readlines methods for http.
>
> Thanks in advance for any help/pointers.
>
> david lees
>
> h = httplib.HTTP("192.168.0.3:50006")
> h.putrequest('POST','/rpchandler HTTP/1.0')
> h.putheader('Content-Type', 'text/xml')
> h.putheader("Content-length", "%d" % len(bod))
> h.endheaders()
> h.send(bod)
> reply, msg, hdrs = h.getreply()
> print reply
> print msg
> print hdrs
> f = h.getfile()
>
> #data=f.read()
> #print data
>
> #data=f.readlines()
> #print data
>
> for i in range(11):
> data=f.readline()
> print data
>
>
> #while 1:
> # data=f.readline()
> # if not data:
> # break
> # print data
>
> f.close()
> h.close()
More information about the Python-list
mailing list