Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. # server.py
  2.  
  3. import socket # Import socket module
  4. import ssl
  5. import sys # Import system-specific parameters and functions
  6. import hashlib
  7. import os.path # Import module to manipulate paths
  8. import os, time, _thread as thread
  9.  
  10. from threading import Thread
  11. from SocketServer import ThreadingMixIn
  12. socket.gethostbyname
  13. TCP_IP = 'localhost'
  14. TCP_PORT = 9001
  15. BUFFER_SIZE = 1024
  16.  
  17. def now():
  18. return time.asctime()
  19.  
  20. def myrec(the_socket):
  21. total_data=[]
  22. while True:
  23. data = the_socket.recv(8192)
  24. if not data: break
  25. total_data.append(data)
  26. return ''.join(total_data)
  27.  
  28.  
  29.  
  30. class ClientThread(Thread):
  31.  
  32. def __init__(self,ip,port,sock):
  33. Thread.__init__(self)
  34. self.ip = ip
  35. self.port = port
  36. self.sock = sock
  37. print " New thread started for "+ip+":"+str(port)
  38.  
  39. def run(self):
  40. user_names = []
  41. passwords = []
  42.  
  43.  
  44. #user_names, passwords = process_file("pass.txt")
  45.  
  46. user = self.sock.recv(1024) # Get up to 1024 bytes at a time
  47. print "User: ", user
  48.  
  49. try:
  50. file_conn = open("pass.txt")
  51. data = file_conn.readlines()
  52.  
  53. for i in range(len(data)):
  54. if i%2 == 0:
  55. user_names.append(data[i][:-1])
  56. else:
  57. passwords.append(data[i][:-1])
  58.  
  59. file_conn.close()
  60. except:
  61. sys.exit('There was a problem reading the file!')
  62.  
  63.  
  64.  
  65. if user not in user_names:
  66. self.sock.send ("Unkown User Name, terminating... ")
  67. sys.exit('Unkown User Name, terminating... n')
  68.  
  69.  
  70. else:
  71. self.sock.send ("Please enter password: ")
  72. password = self.sock.recv(1024) # Get up to 1024 bytes at a time
  73. print "Password sent to server." + password
  74. user_input = hashlib.sha224(password).hexdigest()
  75. dir_name = ((r'/home/%s') % (user))
  76.  
  77. os.chdir(dir_name)
  78.  
  79. if user_input != passwords[user_names.index(user)]:
  80.  
  81.  
  82. print 'Incorrect Password!n'
  83. self.sock.send ("Incorrect password. ")
  84.  
  85.  
  86. sys.exit('Incorrect Password, terminating... n')
  87. else:
  88.  
  89. print 'User is logged in!n'
  90. self.sock.send ('User is logged in!n')
  91.  
  92. filename='se.py'
  93. f = open(filename,'rb')
  94. while True:
  95. l = f.read(BUFFER_SIZE)
  96. while (l):
  97. self.sock.send(l)
  98. #print('Sent ',repr(l))
  99. l = f.read(BUFFER_SIZE)
  100. if not l:
  101. f.close()
  102. self.sock.close()
  103. self.sock.close()
  104.  
  105. tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  106. tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  107. tcpsock.bind((TCP_IP, TCP_PORT))
  108. threads = []
  109.  
  110. while True:
  111. tcpsock.listen(5)
  112. print "Waiting for incoming connections..."
  113. (conn, (ip,port)) = tcpsock.accept()
  114. print 'Got connection from ', (ip,port)
  115. newthread = ClientThread(ip,port,conn)
  116. newthread.start()
  117. threads.append(newthread)
  118.  
  119. for t in threads:
  120. t.join()
  121. # client2.py
  122. #!/usr/bin/env python
  123.  
  124.  
  125. import socket # Import socket module
  126. import sys # Import system-specific parameters and functions
  127. import os.path # Import module to manipulate paths
  128. import ssl
  129. import pprint
  130.  
  131. TCP_IP = 'localhost'
  132. TCP_PORT = 9001
  133. BUFFER_SIZE = 1024
  134.  
  135. def myrec(the_socket):
  136. total_data=[]
  137. while True:
  138. data = the_socket.recv(8192)
  139. if not data: break
  140. total_data.append(data)
  141. return ''.join(total_data)
  142.  
  143. print 'nUser & Password Authenticationn'
  144.  
  145.  
  146. ssl_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  147. ssl_sock.connect((TCP_IP, TCP_PORT))
  148.  
  149. print 'file opened'
  150. user = raw_input('Please Enter User Name: ')
  151. print 'name send to server: ' + user
  152. ssl_sock.send(user)
  153.  
  154. data = ssl_sock.recv(1024)
  155. print data
  156.  
  157. if data == "Unkown User Name, terminating... ":
  158. sys.exit('Unknown user name, terminating... n')
  159.  
  160. else:
  161. # data = ssl_sock.recv(2048)
  162.  
  163. password= raw_input(data)
  164. print 'pas send to server: ' + password
  165. ssl_sock.send(password)
  166.  
  167. data = ssl_sock.recv(2048)
  168. if data == "User is logged in!n":
  169. dir_name = ((r'/home/client/%s') % (user))
  170.  
  171. os.chdir(dir_name)
  172.  
  173. print 'Client is logged in!n'
  174.  
  175. with open('received_file', 'wb') as f:
  176. print 'file opened'
  177. while True:
  178. #print('receiving data...')
  179. data = ssl_sock.recv(BUFFER_SIZE)
  180. print('data=%s', (data))
  181. if not data:
  182. f.close()
  183. print 'file close()'
  184. break
  185. # write data to a file
  186. f.write(data)
  187.  
  188.  
  189. # print "exit client"
  190.  
  191. elif data == "Incorrect password. ":
  192.  
  193. # print 'Incorrect password. n'
  194. sys.exit('Incorrect password. n')
  195.  
  196. else:
  197. print "Terminating... n"
  198.  
  199.  
  200. print('Successfully get the file')
  201. ssl_sock.close()
  202. print('connection closed')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement