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

Derrick 'dman' Hudson dman@dman.ddts.net
Sat, 13 Jul 2002 00:35:03 -0500


--rz+pwK2yUstbofK6
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Jul 12, 2002 at 01:09:36AM +0200, Gregor Lingl wrote:

| > import urllib
| > def randombytes(n=3D128):
| >     urlstr=3D"http://www.fourmilab.ch/cgi-bin/uncgi/Hotbits?nbytes=3D128&fmt=3Dbin
|=20
| Can somebody explain how to accomplish this in Python?

Just make a proper HTTP request.  For example :

$ echo -e 'GET /cgi-bin/uncgi/Hotbits?nbytes=3D128&fmt=3Dbin HTTP/1.0\n\n' =
| netcat www.fourmilab.ch www
HTTP/1.1 200 OK
Date: Sat, 13 Jul 2002 05:20:13 GMT
Server: Apache/2.0.39 (Unix)
Pragma: no-cache
Connection: close
Content-Type: application/octet-stream

=C2A=BA=8AcT=E6$,=A6=E9(=10q&=D2=AA7=94=AB=CF=E8H=EFU=EAr=9ED=AC=D0=D5N`=BB=
,=EE=AB0=16=BC=EFty=10=F7:e=ADZ#Q=98c=A7=8E=84=8F=FB=C7%=96=F7=9D\=01=9AA#=
=AE(xx=D1W=87=0F=FB=F4=13=DES=93=BD=04=01J=E5=F4=15=060)=A2=8AP=EC=08=DF=14=
h=B0=B1q=94=B0=C0P=18F=F2=DA=DAE=1D=E9=01=D5=C1=03e9=CB=87=9B=FB=AB%


One solution would be to open a socket directly, send that line
(followed by a blank line), read the result and skip everything up to
and including the first blank line.  The remainder will be the data.
(of course, one could also use os.popen() and the command above, but
that isn't as fun ;-))

This could also be done with httplib (or maybe urllib), but I don't
have enough experience with it to right the code without RTFMing and
testing as I go.

HTH,
-D

--=20
=20
If anyone would come after me, he must deny himself and take up his
cross and follow me.  For whoever wants to save his life will lose it,
but whoever loses his life for me and for the gospel will save it.  What
good is it for a man to gain the whole world, yet forfeit his soul?  Or
what can a man give in exchange for his soul?
        Mark 8:34-37
=20
http://dman.ddts.net/~dman/


--rz+pwK2yUstbofK6
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj0vvAcACgkQO8l8XBKTpRQXFACgtxz3c+xmzsDPtPb+fRGMOxw2
orcAoI6bbdxzp46Xy8R9/UIhCrcdw1tM
=lNmR
-----END PGP SIGNATURE-----

--rz+pwK2yUstbofK6--