Advertisement
Guest User

Untitled

a guest
Sep 30th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import socket, sys , hashlib, struct
  4.  
  5. unpacker = struct.Struct('6s 6s')
  6. packer = struct.Struct('6s 6s')
  7. server_address = ('localhost', 10000)
  8.  
  9. #File open and output
  10. filename1='text1.txt'
  11. f1 = open(filename1,'r')
  12. text1 = f1.read()
  13. filename2='text2.txt'
  14. f2 = open(filename2,'r')
  15. text2 = f2.read()
  16.  
  17. print '=========================================='
  18. print 'Text1 is :'
  19. print text1
  20. print '========================================='
  21. print 'Text2 is :'
  22. print text2
  23. print '=========================================\n'
  24.  
  25. #Hash creation and output
  26. h = hashlib.sha1()
  27.  
  28. h.update(text1)
  29. hash_text1_client= h.hexdigest()
  30.  
  31. h.update(text2)
  32. hash_text2_client= h.hexdigest()
  33.  
  34. print '=========================================='
  35. print 'Hash for Text1 is :'
  36. print hash_text1_client
  37. print '========================================='
  38. print 'Hash for Text2 is :'
  39. print hash_text2_client
  40. print '=========================================\n'
  41.  
  42. #Connection with the server
  43. print >>sys.stderr, 'Trying to connect to %s:%s' % server_address
  44. try:  
  45.   sock_auth = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  46.   sock_auth.connect(server_address)
  47. except:
  48.   print 'Could not connect to the server.'
  49.   sys.exit(0)
  50.  
  51. print 'Connection Successful.'
  52.  
  53. #Auth step
  54. authenticated_successfully="False"
  55. kicked_out="False"
  56.  
  57. while  ( authenticated_successfully !="True" ) :
  58.   input_username=raw_input('Enter your username :')
  59.   input_password=raw_input('Enter your password :')
  60.   input_credentials = (input_username,input_password)
  61.   input_packed_data = packer.pack(*input_credentials)
  62.   sock_auth.sendall(input_packed_data)
  63.   authenticated_successfully=sock_auth.recv(4)
  64.   kicked_out=sock_auth.recv(4)
  65.   if (kicked_out=="True") :
  66.     sys.exit(0)
  67.  
  68. sock_auth.close()
  69.  
  70. try:
  71.   sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  72.   sock.connect(server_address)
  73.   sock.sendall(text1)
  74.   sock.close()
  75.  
  76.   sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  77.   sock2.connect(server_address)
  78.   sock2.sendall(text2)
  79.   sock2.close()
  80.  
  81.   sock_hash = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  82.   sock_hash.connect(server_address)
  83.   sock_hash.sendall(hash_text1_client)
  84.   sock_hash.sendall(hash_text2_client)
  85.   sock_hash.close()  
  86.  
  87.   sock3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  88.   sock3.connect(server_address)
  89.   result=''
  90.   while True:
  91.     data = sock3.recv(16)
  92.     print >>sys.stderr, 'Received "%s" from data1' % result
  93.     if data:
  94.       result+=data
  95.     else:
  96.       break
  97.   print 'Data Received Successfully.\n'
  98.   print result
  99. finally:
  100.   print 'All done \n'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement