FW: [Tutor] Trying to d0 HTTP GET

Kent Johnson kent37 at tds.net
Tue Apr 19 19:33:08 CEST 2005

Ertl, John wrote:
> All,
> I have figured out a bit more.  I can get the binary values from the service
> but I think they come back as a single string.  How do I read that into an
> array?  The code below will read the first number into the array and print
> it out but how would I read the whole thing into an array...I would like to
> skip the step of putting the raw binary numbers into a variable and instead
> read it directly into the binvalues array.
> I have tried things like  binvalues.read(rawData.read(4,size of array)) and
> a few other things but none of them work.  I was hoping for a fromstream but
> no luck no that either.

Unless the data is huge you should probably just read it all into a string, then pass the string to 
binvalue like this:

binvalues = array.array('f')
rawData = urllib2.urlopen(...).read()


This is likely to be the fastest approach as all the looping happens internally to urllib2 and 
array. The only limitation is that both representations have to fit in memory at once.

Alternately you could wrap the rawData in a generator function which returns floats. Then pass the 
generator to binvalues.extend(). Something like this (untested):

import array, struct, urllib2

def generateFloats(rawDataStream):
   while True:
     s = rawData.read(4)
     if len(s) < 4: return
     f = struct.unpack('f', s) # prefix the 'f' with the correct byte-order character...
     yield f

binvalues = array.array('f')
rawDataStream = urllib2.urlopen(...)



> Thanks for any help.
> <CODE>
> binvalues = array.array('f')
> rawData =
> urllib2.urlopen("http://dsd1u:7003/GRID:U:NOGAPS:2005041800:global_360x181:a
> ir_temp:ht_sfc:00020000:00000000:fcst_ops:0240")
> binvalues.fromstring(rawData.read(4))  # 4 byte float
> binvalues.byteswap()
> print binvalues
> </CODE>

More information about the Tutor mailing list