[Tutor] A question on getting binary data from the net

Gregor Lingl glingl@aon.at
Fri, 12 Jul 2002 02:58:15 +0200


> 
> I think, of course, this could be done in two or three lines if
> one knew how to fetch binary data from the net - using this form
> of the url:
> 
> http://www.fourmilab.ch/cgi-bin/uncgi/Hotbits?nbytes=128&fmt=bin
> 
> Can somebody explain how to accomplish this in Python?
> 
> Gregor
> 

What about this?

import urllib
def randombytes(n=128):
    if n>2048: print "Maximum of 2048 bytes returned"
    urlstr="http://www.fourmilab.ch/cgi-bin/uncgi/Hotbits?"+\
                                      "nbytes=%d&fmt=bin"%n
    return [ord(c) for c in urllib.urlopen(urlstr).read(n)]

Gregor
 
P.S. or should I use urlretrieve instead? But, then, how
do I prevent it from writing to disk?