Advertisement
Titokhan

receiverwithhash.py

Jun 13th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. from socket import *
  2. import sys
  3. import select
  4. import hashlib
  5.  
  6. host="0.0.0.0"
  7. port = 9999
  8. s = socket(AF_INET,SOCK_DGRAM)
  9. s.bind((host,port))
  10.  
  11. addr = (host,port)
  12. buf=1024
  13.  
  14. data,addr = s.recvfrom(buf)
  15. print "Received File:",data.strip()
  16. f = open(data.strip(),'wb')
  17.  
  18. data,addr = s.recvfrom(buf)
  19. try:
  20.     while(data):
  21.         f.write(data)
  22.         s.settimeout(2)
  23.         data,addr = s.recvfrom(buf)
  24. except timeout:
  25.     f.close()
  26.     s.close()
  27.     print "File Downloaded"
  28.  
  29.  
  30. file_name= raw_input("Enter file name:  ")
  31. desired_hash= raw_input("Enter desired hash:  ")
  32. BLOCKSIZE = 65536
  33. hasher = hashlib.sha1()
  34. with open(file_name, 'rb') as afile:
  35.     buf = afile.read(BLOCKSIZE)
  36.     while len(buf) > 0:
  37.         hasher.update(buf)
  38.         buf = afile.read(BLOCKSIZE)
  39. current_hash=hasher.hexdigest()
  40. if(desired_hash==current_hash):
  41.            print "File is intact"
  42. else:
  43.            print "File is broken"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement