Skip to content

Instantly share code, notes, and snippets.

@chenshuo
Created June 4, 2012 15:55
Show Gist options
  • Select an option

  • Save chenshuo/2869173 to your computer and use it in GitHub Desktop.

Select an option

Save chenshuo/2869173 to your computer and use it in GitHub Desktop.
trivial file download
#!/usr/bin/python
import socket, os, hashlib, time
md5 = hashlib.md5()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('server_address', 2021))
start = time.time()
length = 0
out = open('/tmp/download', 'wb')
while True:
message = sock.recv(65536)
if message:
length += len(message)
md5.update(message)
out.write(message)
else:
print "done"
sock.close()
out.close()
break
seconds = time.time() - start
print md5.hexdigest()
print length, " bytes"
print "%.3f seconds" % (seconds)
print "%.3f MiB/s" % (length/seconds/1024/1024)
$ time ./download.py
done
7c148e0a1b330186adef66ee3e2d433d
4238800896 bytes
36.339 seconds
111.243 MiB/s
real 0m36.357s
user 0m13.490s
sys 0m14.390s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment